Exporting BarChart

4 posts, 0 answers
  1. Ghasem
    Ghasem avatar
    15 posts
    Member since:
    May 2014

    Posted 25 Dec 2014 Link to this post

    Hi All, 
    I am using WPF RadChart , to create and export charts in code behind, everything is ok in all chart type same as bubble , pie , linear, … but in Bar chart using BarSeriesDefinition, the exported Image has no bar! Labels are shown correctly above hidden bars! Also created chart is shown correctly in WPF RadChart.
    in this below code I show the problem
    Regards


    private void Button_Click(object sender, RoutedEventArgs e)
            {
                ChartBaseParameter ChartParam = new ChartBaseParameter()
                {
                    XAxisField = "x",
                    YAxisFields = new List<string>() { "y" }
                };
     
                DataTable dt = new DataTable();
                dt.Columns.Add("x", typeof(int));
                dt.Columns.Add("y", typeof(int));
                dt.Rows.Add(1, 1);
                dt.Rows.Add(2, 2);
     
                this.ChartPanel.SeriesMappings.Clear();
                this.ChartPanel.SortDescriptors.Clear();
                this.ChartPanel.ItemsSource = null;
     
                var chart = InitChart(this.ChartPanel, ChartParam, true, dt);
                var fileName = "C:\\myChart.jpg";
                chart.ExportToImage(fileName, new PngBitmapEncoder());
            }
     
        
            public static RadChart InitChart(RadChart chartPanel, ChartBaseParameter chartParam, bool showError, System.Data.DataTable chartTable)
            {
                bool showItemLabels = true;
     
                AnimationManager.IsGlobalAnimationEnabled = false;
                chartPanel.DefaultView.ChartArea.EnableAnimations = false;
     
                chartPanel.BeginInit();
                chartPanel.Width = 300;
                chartPanel.Height = 300;
     
                BarLabelSettings barLabelSetting = new BarLabelSettings();
                barLabelSetting.Distance = 10;
                barLabelSetting.LabelDisplayMode = LabelDisplayMode.Outside;
                barLabelSetting.ShowConnectors = true;
     
                foreach (var YItem in chartParam.YAxisFields)
                {
     
                    SeriesMapping seriesmappingi = new SeriesMapping() { LegendLabel = YItem };
                    seriesmappingi.SeriesDefinition = new BarSeriesDefinition() { ShowItemLabels = showItemLabels, LabelSettings = barLabelSetting };
                    seriesmappingi.ItemMappings.Add(new ItemMapping("y", DataPointMember.YValue) { FieldType = typeof(int) });
                    seriesmappingi.ItemMappings.Add(new ItemMapping("x", DataPointMember.XCategory) { FieldType = typeof(int) });
                    seriesmappingi.ItemMappings.Add(new ItemMapping("x", DataPointMember.LegendLabel) { FieldType = typeof(int) });
                    chartPanel.SeriesMappings.Add(seriesmappingi);
                }
     
                chartPanel.DefaultView.ChartArea.AxisX.LabelRotationAngle = -90;
                chartPanel.DefaultView.ChartArea.ZoomScrollSettingsX.MinZoomRange = 0.01;
                chartPanel.DefaultView.ChartArea.AxisX.Title = chartParam.XAxisField;
     
                chartPanel.DefaultView.ChartArea.ZoomScrollSettingsX.ScrollMode = ScrollMode.ScrollAndZoom;
                chartPanel.DefaultView.ChartArea.AxisY.ExtendDirection = AxisExtendDirection.Both;
                chartPanel.DefaultView.ChartArea.LabelFormatBehavior = LabelFormatBehavior.None;
     
                chartPanel.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
     
                chartPanel.EndInit();
                 
                chartPanel.Measure(new Size(1024, 768));
                chartPanel.Arrange(new System.Windows.Rect(new Point(0, 0), chartPanel.DesiredSize));
                 
                chartPanel.ItemsSource = chartTable;
                chartPanel.Rebind();
                chartPanel.UpdateLayout();
                return chartPanel;
            }
     
        }
        public class ChartBaseParameter
        {
            public string XAxisField;
            public List<string> YAxisFields;
        }

  2. Peshito
    Admin
    Peshito avatar
    497 posts

    Posted 26 Dec 2014 Link to this post

    Hello,

    What you can try is first disabling all animations:
    Copy Code
    chart.DefaultView.ChartArea.EnableAnimations = false;
    Also, when using bar series you will need to set an ItemStyle to the bar SeriesDefinition which excludes animations specific to the Bars.

    Hope this helps.


    Regards,
    Peshito
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ghasem
    Ghasem avatar
    15 posts
    Member since:
    May 2014

    Posted 27 Dec 2014 in reply to Peshito Link to this post

    thank you for your reply , but i don't know which property I should use to exclude animation, I try below but it doesn't work
    seriesmappingi.SeriesDefinition = new BarSeriesDefinition()
                                    {
                                        ShowItemLabels = showItemLabels,
                                        AnimationSettings = new AnimationSettings()
                                        {
                                            DefaultSeriesDelay = TimeSpan.Zero,
                                            TotalSeriesAnimationDuration = TimeSpan.Zero,
                                            ItemAnimationDuration = TimeSpan.Zero,
                                            ItemDelay = TimeSpan.Zero
                                        }
                                    };
  5. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 31 Dec 2014 Link to this post

    Hi Ghasem,

    The trick to exporting a bar chart is setting a style in which the animations have been removed, just as mentioned earlier. I have attached a simple project which successfully exports a bar chart. You will find the bar ItemStyle in the code. I hope this helps.

    Regards,
    Petar Marchev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top