c# - 如何使用c#绘制图表?

标签 c# database vb.net winforms charts

我必须使用 C# 绘制条形图。首先,我尝试使用 vb.net 使用 System.Windows.Forms.DataVisualization.Charting namespace.code 如下

    Dim strConn As  string MyConString = "SERVER=localhost;" + "DATABASE=demo;" + "UID=root;" + "PASSWORD=root;";
    Dim conn As New SqlConnection(strConn)

    Dim sqlProducts As String = "SELECT Top 8 ProductName, UnitsInStock FROM Products"
    Dim da As New MYSqlDataAdapter(sqlProducts, conn)
    Dim ds As New DataSet()
    da.Fill(ds, "Products")

    Dim ChartArea1 As ChartArea = New ChartArea()
    Dim Legend1 As Legend = New Legend()
    Dim Series1 As Series = New Series()
    Dim Chart1 = New Chart()
    Me.Controls.Add(Chart1)

    ChartArea1.Name = "ChartArea1"
    Chart1.ChartAreas.Add(ChartArea1)
    Legend1.Name = "Legend1"
    Chart1.Legends.Add(Legend1)
    Chart1.Location = New System.Drawing.Point(13, 13)
    Chart1.Name = "Chart1"
    Series1.ChartArea = "ChartArea1"
    Series1.Legend = "Legend1"
    Series1.Name = "Series1"
    Chart1.Series.Add(Series1)
    Chart1.Size = New System.Drawing.Size(800, 400)
    Chart1.TabIndex = 0
    Chart1.Text = "Chart1"

    Chart1.Series("Series1").XValueMember = "date"
    Chart1.Series("Series1").YValueMembers = "temperature"

    Chart1.DataSource = ds.Tables("flowchart")

它的工作原理与我尝试使用 c# 时完全一样

        string MyConString = "SERVER=localhost;" + "DATABASE=demo;" + "UID=root;" + "PASSWORD=root;";
        MySqlConnection con = new MySqlConnection(MyConString);
        con.Open();
        string s = "select date,temperature from flowchart";
        MySqlDataAdapter da = new MySqlDataAdapter(s, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "flowchart");

        ChartArea chartarea1 = new ChartArea();
        Legend legend = new Legend();
        Chart c1 = new Chart();
        Series series = new Series();
        Controls.Add(c1);

        chartarea1.Name = "ChartArea";
        c1.ChartAreas.Add(chartarea1);
        legend.Name = "Legend";
        c1.Legends.Add(legend);
        c1.Location = new System.Drawing.Point(13, 13);
        series.Name = "Series";
        c1.Series.Add(series);
        c1.Size = new System.Drawing.Size(800, 400);
        c1.TabIndex = 0;
        c1.Text = "Chart1";

        c1.Series("Series").XValueMember = "date";
        c1.Series("Series").YValueMembers = "temperature";
        c1.DataSource = ds.Tables("flowchart");

最后三行出现错误。我不知道这是正确的方法。

最佳答案

在 C# 中,当您在列表/集合或数组中提供参数时,应将其写在方括号 [] 中。只有方法或函数的参数可以有圆括号()。 `

应该这样写

c1.Series["Series"].XValueMember = "date";`

关于c# - 如何使用c#绘制图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23053620/

相关文章:

c# - 使用 Web 部署在 Azure 中部署普通 ASP.NET Core 2.2 Web 应用程序会引发错误

database - Android SQLite,询问ContentValues

sql - 检索 "Create Table As"语句中使用的脚本

vb.net - 在 VB 中,如何强制继承类使用类的属性?

asp.net - DropDownList SelectedIndex 值未在 AutoPostback 上更新

c# - 在 ASP .NET 应用程序中每 24 小时/每个用户自动触发事件一次

c# - 如何将一定比例的用户重定向到 Beta 测试网站?

database - 最好的网站/网络应用程序升级过程是什么?

vb.net - vs 2010读取配置设置

c# - 抽象自动映射器预测