VS2013动态创建图表c#

时间:2015-01-30 16:09:50

标签: c# charts

无法创建标准图表。我试过这些方法: http://timbar.blogspot.com/2012/04/creating-chart-programmatically-in-c.html

Chart creating dynamically. in .net, c#

例如代码:

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();
}

但它让我空洞。

如何制作图表?请帮忙!

1 个答案:

答案 0 :(得分:0)

您需要将图表添加到表单中:

this.Controls.Add(图表);