Answered How to create a new chart based on another chart in code?

  • 25 มิถุนายน 2555 6:22
     
     

    How to create a new chart based on another chart in code?

    There is a chart has been set appearance ,I want create a new chart with the same appearance and different datasource.How can I do this in .cs file?

ตอบทั้งหมด

  • 25 มิถุนายน 2555 21:08
     
     คำตอบ มีโค้ด

    I am not sure whether this is the best way to do it but its a workaround for now. Try this code and set the attributes of the chart manually.

                Chart chart2 = new Chart();
                chart2.ChartAreas.Add(Chart1.ChartAreas["ChartArea1"]);
                foreach (Series series in Chart1.Series)
                {
                    chart2.Series.Add(series);
                }
                foreach (Series series in chart2.Series)
                {
                    series.Points.Clear();
                    //Add new points manually.
                }
                chart2.Titles.Add(Chart1.Titles["Title1"]);
                chart2.Legends.Add(Chart1.Legends["Default"]);
                chart2.Series["Series1"].ShadowColor = Chart1.Series["Series1"].ShadowColor;
                chart2.Series["Series1"].ShadowOffset = Chart1.Series["Series1"].ShadowOffset;
                chart2.Palette = Chart1.Palette;
                chart2.BackColor = Chart1.BackColor;
                chart2.BackHatchStyle = Chart1.BackHatchStyle;
                chart2.BackGradientStyle = Chart1.BackGradientStyle;
                chart2.BorderlineDashStyle = Chart1.BorderlineDashStyle;
                chart2.BorderlineWidth = Chart1.BorderlineWidth;
                chart2.BorderlineColor = Chart1.BorderlineColor;
                chart2.BorderSkin = Chart1.BorderSkin;
                chart2.Width = Chart1.Width;
                chart2.Height = Chart1.Height;
                Form1.Controls.Add(chart2);

    Hope this helps,


    If this post answers your question "MARK AS ANSWER" If this post helps you "VOTE AS HELPFUL"

    • ทำเครื่องหมายเป็นคำตอบโดย Ken767 7 กรกฎาคม 2555 2:04
    •  
  • 26 มิถุนายน 2555 14:18
     
     คำตอบ มีโค้ด

    There is a chart serializer that lets you save the appearence (as well as other aspects) of a chart as a file, using something like this

    aChart.Serializer.Content = SerializationContents.Appearance;
    FileStream output = new FileStream(fileName , FileMode.OpenOrCreate , FileAccess.Write);
    aChart.Serializer.Save(output);
    output.Close();

    You can then apply it to another chart,

    FileStream input = new FileStream(fileName , FileMode.Open , FileAccess.Read);
    aChart.LoadTemplate(input);

    If you are just interested in the colours there is also the Palette feature,

    cChart.Palette = ChartColorPalette.Excel;


    George
    http://www.sharpstatistics.co.uk/blog

    • ทำเครื่องหมายเป็นคำตอบโดย Ken767 7 กรกฎาคม 2555 2:03
    •