【问题标题】:C#. WPF. Problems with Chart BuildingC#。 WPF。图表构建问题
【发布时间】:2015-05-20 16:22:43
【问题描述】:

在我的图表构建 WPF 应用程序中,我尝试使用 WinForms System.Windows.Forms.DataVisualization.Charting;

我有以下代码:

private void build()
{
    for(int i = 0; i< boundData.Count; i++)
    {
        var chart = new Chart();
        var ser = new Series();

        ser.Name = "Series1";
        ser.ChartType = SeriesChartType.Bar;
        ser.Color = System.Drawing.Color.Blue;

        chart.Series.Add(ser);
        charts.Add(chart);

        for (int j = 0; j < boundData[i].Count; j++)
        {
            charts[i].Series["Series1"].Points.AddXY(j, boundData[i][j]);
        }
    }
        //Just for tesing Purposes
        charts[1].Invalidate();
        charts[1].SaveImage("met.png", ChartImageFormat.Png);
}

'boundData' 具有以下结构

List<List<int>>:
List<int> : [ 1,2,3,4,5,6,... ];
List<int> : [ 1,2,3,4,5,6, ... ];
...

根据 debagger,'Series' 属性有分数。

我想做的是建立一个图表并将其保存为图像。保存的图像什么都没有(空) 谁能指出我做错了什么或建议另一种构建图表的方法?

【问题讨论】:

    标签: c# wpf charts


    【解决方案1】:

    您需要在绘制之前添加一个 ChartArea

        private void build()
        {
            for (int i = 0; i < boundData.Count; i++)
            {
                var chart = new Chart();
                var ser = new Series();
    
                chart.ChartAreas.Add(new ChartArea("Area1"));
    
                ser.ChartArea = "Area1";
                ser.Name = "Series1";
                ser.ChartType = SeriesChartType.Bar;
                ser.Color = System.Drawing.Color.Blue; ;
                chart.Series.Add(ser);
                charts.Add(chart);
    
                for (int j = 0; j < boundData[i].Count; j++)
                {
                    charts[i].Series["Series1"].Points.AddXY(j, boundData[i][j]);
                }
            }
            //Just for tesing Purposes
            charts[1].Invalidate();
            charts[1].SaveImage("met.png", ChartImageFormat.Png);
        }
    

    【讨论】:

      【解决方案2】:

      当您使用 wpf 时,请使用 oxyplot 而不是 windows 窗体。它是一个开源项目。你可以找到它的演示here。它还支持将图表另存为图像。

      示例:https://github.com/oxyplot/oxyplot/tree/master/Source/Examples

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-13
        • 2011-05-20
        • 1970-01-01
        • 2016-06-11
        • 2015-11-06
        • 1970-01-01
        相关资源
        最近更新 更多