在winform上创建折线图时出错

时间:2017-04-26 05:21:55

标签: c# .net winforms graph

Error image 尝试使用我的图表初始化表单时,我一直收到此错误。无法找到解决方法。认为它与数据绑定有关

有什么想法吗?

这是我的代码 -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace NoCAnalysisTool2
{

    public partial class Visualisation : UserControl
    {
        public string tx_graph { get; set; }
        public List<int> tx_graphData { get; set; }
        public Visualisation(string txGraph, List<int> tx_GData)
        {
            InitializeComponent();
            tx_graph = txGraph;
            tx_graphData = tx_GData;

            // Set 3D chart settings
            chart1.ChartAreas["Default"].Area3DStyle.Enable3D = true;
            chart1.ChartAreas["Default"].Area3DStyle.IsRightAngleAxes = false;
            chart1.ChartAreas["Default"].Area3DStyle.Inclination = 40;
            chart1.ChartAreas["Default"].Area3DStyle.Rotation = 20;
            chart1.ChartAreas["Default"].Area3DStyle.LightStyle = LightStyle.Realistic;

            // Populate series with random data
            Random random = new Random();
            for (int pointIndex = 0; pointIndex < 10; pointIndex++)
            {
                chart1.Series["Series1"].Points.AddY(random.Next(45, 95));
                chart1.Series["Series2"].Points.AddY(random.Next(5, 75));
            }

            // Set series chart type
            chart1.Series["Series1"].ChartType = SeriesChartType.Line;
            chart1.Series["Series2"].ChartType = SeriesChartType.Spline;

            // Set point labels
            chart1.Series["Series1"].IsValueShownAsLabel = true;
            chart1.Series["Series2"].IsValueShownAsLabel = true;

            // Enable X axis margin
            chart1.ChartAreas["Default"].AxisX.IsMarginVisible = true;

            // Enable the ShowMarkerLines
            chart1.Series["Series1"]["ShowMarkerLines"] = "true";
            chart1.Series["Series2"]["ShowMarkerLines"] = "true";
        }

        private void TxGraph_Load(object sender, EventArgs e)
        {

        }


        private void Tx_graph_Click(object sender, EventArgs e)
        {


        }


        private void Visualisation_Load(object sender, EventArgs e)
        {
        }


            private void chart11_Click(object sender, EventArgs e)
            {

            }

    }
}

1 个答案:

答案 0 :(得分:0)

'默认' ChartArea不是"Default",而是"ChartArea1"

尝试使用错误的名称访问索引器会导致Argument Exeption。

您可以选择通过索引调用它:

ChartArea ca = chart1.ChartAreas[0];

或者正确名称:

ChartArea ca = chart1.ChartAreas["ChartArea1"];

或者将Name属性设置为您喜欢的字符串..:

chart1.ChartAreas[0].Name = "Default";

..然后通过Name呼叫它:

chart1.ChartAreas["Default"].Area3DStyle.Enable3D = true;
顺便说一下:我希望你在设计师中添加了第二系列并给它正确的名字; - )