简体   繁体   English

VS2013动态创建图表C#

[英]VS2013 Dynamically create chart c#

Can't create standart chart. 无法创建标准图表。 I tried these methods: http://timbar.blogspot.com/2012/04/creating-chart-programmatically-in-c.html 我尝试了以下方法: http : //timbar.blogspot.com/2012/04/creating-chart-programmatically-in-c.html

Chart creating dynamically. 动态创建图表。 in .net, c# 在.net中,C#

For example code: 例如代码:

private void Form2_Load(object sender, EventArgs e)
    {

var xvals = new[]
            {
                new DateTime(2012, 4, 4), 
                new DateTime(2012, 4, 5), 
                new DateTime(2012, 4, 6), 
                new DateTime(2012, 4, 7)
            };
        var yvals = new[] { 1,3,7,12 };

        // create the chart
        var chart = new Chart();
        chart.Size = new Size(600, 250);

        var chartArea = new ChartArea();
        chartArea.AxisX.LabelStyle.Format = "dd/MMM\nhh:mm";
        chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
        chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
        chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
        chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
        chart.ChartAreas.Add(chartArea);

        var series = new Series();
        series.Name = "Series1";
        series.ChartType = SeriesChartType.FastLine;
        series.XValueType = ChartValueType.DateTime;
        chart.Series.Add(series);

        // bind the datapoints
        chart.Series["Series1"].Points.DataBindXY(xvals, yvals);

        // copy the series and manipulate the copy
        chart.DataManipulator.CopySeriesValues("Series1", "Series2");
        chart.DataManipulator.FinancialFormula(
            FinancialFormula.WeightedMovingAverage, 
            "Series2"
        );
        chart.Series["Series2"].ChartType = SeriesChartType.FastLine;

        // draw!
        chart.Invalidate();
}

But it gives me empty form. 但这给了我空的形式。

How to create chart? 如何创建图表? Please help! 请帮忙!

You need to add the chart to your form: 您需要将图表添加到表单中:

this.Controls.Add(chart); this.Controls.Add(图表);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM