Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this is the sample code of dynamically created chart .


C#
private void NewCostVaraianceChart()
        {
            try
            {
              //  listBox2.Width = Application_Masters.FormLoader.set_layout_width();
                if (taskdata != null)
                {
                    tasklist = taskdata.ToList();
                }
                StackPanel stkpanel = new StackPanel();

                int chartwidth = 400;
                int taskcount = 3;

                //if (listBox2.Width < 1200)
                //{
                //    taskcount = 2;
                //    chartwidth = Convert.ToInt32(listBox2.Width / taskcount);
                //    chartwidth = chartwidth - 70;
                //}

                Style style = new Style(typeof(Control));
                ResourceDictionaryCollection palette = new ResourceDictionaryCollection();
                for (int i = 0; i < taskdata.Count; i++)
                {
                    Chart barChart = new Chart();

                    barChart.Style = Application.Current.Resources["ChartStyleSch"] as Style;
                    palette = new ResourceDictionaryCollection();

                    barChart.Palette.Clear();
                    barChart.Axes.Clear();
                    barChart.Series.Clear();

                    if (i % taskcount == 0)
                    {
                        stkpanel = new StackPanel();
                        stkpanel.Orientation = Orientation.Horizontal;
                    }

                    CustomColumnSeries obj_columsrs = new CustomColumnSeries();
                    barChart.Height = 225;
                    barChart.Width = chartwidth;
                    barChart.Margin = new Thickness(10, 5, 10, 5);

                    obj_columsrs.IndependentValuePath = "ProjectName";
                    obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;

                    List<chartdata> cust = new List<chartdata>();

                    cust.Add(new ChartData()
                    {
                        WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
                        ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
                        ProjectName = tasklist[i].Projectname,
                        ColumnColors = new SolidColorBrush(GetRandomColor()),
                        tcolor = new SolidColorBrush(Colors.Blue),
                        ChangedHr = Convert.ToInt32(tasklist[i].Balance),
                        ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
                    });

                    List<chartdata> cust1 = new List<chartdata>();
                    cust1.Add(new ChartData()
                    {
                        WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
                        ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
                        ProjectName = tasklist[i].Projectname,
                        tcolor = new SolidColorBrush(Colors.Blue),
                        ColumnColors = new SolidColorBrush(GetRandomColor()),
                        ChangedHr = Convert.ToInt32(tasklist[i].Balance),
                        ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
                    });
                    List<chartdata> cust2 = new List<chartdata>();
                    cust2.Add(new ChartData()
                    {
                        WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
                        ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
                        ProjectName = tasklist[i].Projectname,
                        tcolor = new SolidColorBrush(Colors.Blue),
                        ColumnColors = new SolidColorBrush(GetRandomColor()),
                        ChangedHr = Convert.ToInt32(tasklist[i].Balance),
                        ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
                    });
                    List<chartdata> cust3 = new List<chartdata>();
                    cust3.Add(new ChartData()
                    {
                        WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
                        ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
                        ProjectName = tasklist[i].Projectname,
                        tcolor = new SolidColorBrush(Colors.Blue),
                        ColumnColors = new SolidColorBrush(GetRandomColor()),
                        ChangedHr = Convert.ToInt32(tasklist[i].Balance),
                        ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
                    });

                    var getMxvalue = 0.0;
                    //if (tasklist[i].Scheduledhr > tasklist[i].TaskWorkedHour)
                    //{
                    //    getMxvalue = Convert.ToInt32(tasklist[i].Scheduledhr);
                    //}
                    //else
                    //{
                    //    getMxvalue = Convert.ToInt32(tasklist[i].TaskWorkedHour);
                    //}

                    if (tasklist[i].Scheduledhr > tasklist[i].TaskWorkedHour && tasklist[i].Scheduledhr > tasklist[i].Balance && tasklist[i].Scheduledhr > tasklist[i].TaskWorkedMin)
                    {
                        getMxvalue = Convert.ToDouble(tasklist[i].Scheduledhr);
                    }
                    else if (tasklist[i].TaskWorkedHour > tasklist[i].Scheduledhr && tasklist[i].TaskWorkedHour > tasklist[i].Balance && tasklist[i].TaskWorkedHour > tasklist[i].TaskWorkedMin)
                    {
                        getMxvalue = Convert.ToDouble(tasklist[i].TaskWorkedHour);
                    }
                    else if (tasklist[i].Balance > tasklist[i].TaskWorkedHour && tasklist[i].Balance > tasklist[i].Scheduledhr && tasklist[i].Balance > tasklist[i].TaskWorkedMin)
                    {
                        getMxvalue = Convert.ToDouble(tasklist[i].Balance);
                    }
                    else if (tasklist[i].TaskWorkedMin > tasklist[i].TaskWorkedHour && tasklist[i].TaskWorkedMin > tasklist[i].Scheduledhr && tasklist[i].TaskWorkedMin > tasklist[i].Balance)
                    {
                        getMxvalue = Convert.ToDouble(tasklist[i].TaskWorkedMin);
                    }

                    var ValToRound = Math.Ceiling(Convert.ToDouble(getMxvalue) / 2);
                    if (ValToRound > 10)
                    {
                        ValToRound = RoundOff(((int)ValToRound));
                    }


                    LinearAxis linax = new LinearAxis();
                    linax.Orientation = AxisOrientation.Y;
                    barChart.Axes.Add(linax);
                    LinearAxis linax1 = barChart.Axes[0] as LinearAxis;

                    linax1.Minimum = 0;
                    linax1.Interval = ValToRound;
                    linax1.Maximum = Math.Ceiling(getMxvalue) + Math.Ceiling(ValToRound);


                    CategoryAxis catax1 = new CategoryAxis();
                    catax1.Orientation = AxisOrientation.Y;
                    barChart.Axes.Add(catax1);

                    CategoryAxis catax = barChart.Axes[1] as CategoryAxis;
                    catax.Title = ResourceFile.HoursLabel;
                    catax1.Location = AxisLocation.Left;

                    obj_columsrs.DependentValuePath = "ScheduledHr";
                    obj_columsrs.Title = "   " + ResourceFile.ScheduledLabel;
                    barChart.Series.Add(obj_columsrs);

                    style = new Style(typeof(Control));
                    style.Setters.Add(new Setter(MaxWidthProperty, 30));
                    style.Setters.Add(new Setter(BackgroundProperty, cust[0].ColumnColors));
                    style.Setters.Add(new Setter(BorderBrushProperty, cust[0].ColumnColors));
                    style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'-45,0,25,0'")));


                    ResourceDictionary dictionary = new ResourceDictionary();
                    dictionary.Add("DataPointStyle", style);
                    palette.Add(dictionary);
                    barChart.Palette = palette;
                    ((CustomColumnSeries)barChart.Series[0]).ItemsSource = cust;



                    obj_columsrs = new CustomColumnSeries();

                    obj_columsrs.IndependentValuePath = "ProjectName";
                    obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
                    obj_columsrs.DependentValuePath = "WorkedHr";
                    obj_columsrs.Title = obj_columsrs.Title = "   " + ResourceFile.WorkedLabel;

                    style = new Style(typeof(Control));
                    style.Setters.Add(new Setter(MaxWidthProperty, 30));
                    style.Setters.Add(new Setter(BackgroundProperty, cust1[0].ColumnColors));
                    style.Setters.Add(new Setter(BorderBrushProperty, cust1[0].ColumnColors));
                    style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'-20,0,0,0'")));

                    dictionary = new ResourceDictionary();
                    dictionary.Add("DataPointStyle", style);
                    palette.Add(dictionary);
                    barChart.Palette = palette;
                    barChart.Series.Add(obj_columsrs);
                    ((CustomColumnSeries)barChart.Series[1]).ItemsSource = cust1;


                    obj_columsrs = new CustomColumnSeries();
                    obj_columsrs.IndependentValuePath = "ProjectName";
                    obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
                    obj_columsrs.DependentValuePath = "ChangedHr";
                    obj_columsrs.Title = obj_columsrs.Title = "   " + ResourceFile.ChangeHour;
                    style = new Style(typeof(Control));
                    style.Setters.Add(new Setter(MaxWidthProperty, 30));
                    style.Setters.Add(new Setter(BackgroundProperty, cust2[0].ColumnColors));
                    style.Setters.Add(new Setter(BorderBrushProperty, cust2[0].ColumnColors));
                    style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'5,0,-25,0'")));

                    dictionary = new ResourceDictionary();
                    dictionary.Add("DataPointStyle", style);
                    palette.Add(dictionary);
                    barChart.Palette = palette;
                    barChart.Series.Add(obj_columsrs);
                    ((CustomColumnSeries)barChart.Series[2]).ItemsSource = cust2;


                    obj_columsrs = new CustomColumnSeries();
                    obj_columsrs.IndependentValuePath = "ProjectName";
                    obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
                    obj_columsrs.DependentValuePath = "ReWorkedHr";
                    obj_columsrs.Title = obj_columsrs.Title = "   " + ResourceFile.ReworkHour;
                    style = new Style(typeof(Control));
                    style.Setters.Add(new Setter(MaxWidthProperty, 30));
                    style.Setters.Add(new Setter(BackgroundProperty, cust3[0].ColumnColors));
                    style.Setters.Add(new Setter(BorderBrushProperty, cust3[0].ColumnColors));
                    style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'30,0,-50,0'")));

                    dictionary = new ResourceDictionary();
                    dictionary.Add("DataPointStyle", style);
                    palette.Add(dictionary);
                    barChart.Palette = palette;
                    barChart.Series.Add(obj_columsrs);
                    ((CustomColumnSeries)barChart.Series[3]).ItemsSource = cust3;


                    stkpanel.Children.Add(barChart);

                    //stckprint.Children.Add(barChart);

                    if (i == taskdata.Count - 1)
                    {
                        stckprint.Children.Add(stkpanel);
                    }
                    else if ((i + 1) % taskcount == 0 && i != 0)
                    {
                        stckprint.Children.Add(stkpanel);
                    }

                }

            }
            catch (Exception)
            {
            }
        }






this is the sample code of print button click








C#
private void Print_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrintDocument pd = new PrintDocument();
                int itemIndex = 0;
                pd.PrintPage += (s, pe) =>
                {

                   // StackPanel itemHost = new StackPanel();
                    while (itemIndex < stckprint.Children.Count)
                    {

                        //Viewbox lstboxprnt = new Viewbox();

                        //lstboxprnt.DataContext = lstbox.Items[itemIndex];
                        try
                        {
                         //   itemHost.Children.Add(stckprint.Children[itemIndex]);

                            stckprint.Measure(new Size(pe.PrintableArea.Width, double.PositiveInfinity));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());

                            //throw;
                        }
                        if (stckprint.DesiredSize.Height > pe.PrintableArea.Height && stckprint.Children.Count > 1 && stckprint.Children.Count >1)
                        {
                            stckprint.Children.Remove(stckprint);
                            pe.HasMorePages = true;
                            break;
                        }
                        if (stckprint.DesiredSize.Width > pe.PrintableArea.Width && stckprint.Children.Count > 1)
                        {
                            stckprint.Children.Remove(stckprint);
                            pe.HasMorePages = true;
                            break;
                        }
                        
                    }
                    pe.PageVisual = stckprint;
                    itemIndex += 1;

                };
                Viewbox viewbox = new Viewbox();
                ItemsControl itemhost = new ItemsControl();
               // itemhost.ItemsSource = stkprin;

                pd.Print("text");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

                //throw;
            }
        }
    }







plase try to give me complete solution based on this code sample that will work fine


thanks and regard.
Vikash
Posted
Updated 29-Mar-14 4:25am
v2
Comments
[no name] 27-Mar-14 10:15am    
Hardly anyone is going to read all of this unformatted code to figure out what it is that you have done. You really need to chop this down to something that is readable and relevant to your problem.
CHill60 29-Mar-14 10:36am    
No-one is going to give you a "complete solution" either.
If your print button works for one chart, why not just call it for each chart you have?

1 solution

"give me complete solution based on this code sample that will work fine"

It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to VWorker.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900