2017-06-01 22 views
0

大家好我有一個問題,我需要爲我的視覺工作室項目使用不同的(樣式)外觀圖表類型(例如:餅圖,柱形圖)。該項目的代碼語言是C#,我有一個HTML腳本。我的問題是:我如何以及在哪裏可以找到並在我的項目中添加新圖表?下面的函數定義的圖表類型等:如何將不同的圖表設計添加到Visual Studio項目中?

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     Chart1.Visible = ddlCountries.SelectedValue != ""; 
     string query = string.Format("select shipcity, count(orderid) from orders where shipcountry = '{0}' group by shipcity", ddlCountries.SelectedValue); 
     DataTable dt = GetData(query); 
     string[] x = new string[dt.Rows.Count]; 
     int[] y = new int[dt.Rows.Count]; 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      x[i] = dt.Rows[i][0].ToString(); 
      y[i] = Convert.ToInt32(dt.Rows[i][1]); 
     } 
     Chart1.Series[0].Points.DataBindXY(x, y); 
     Chart1.Series[0].ChartType = SeriesChartType.Pie; 
     Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true; 
     Chart1.Legends[0].Enabled = true; 
    } 

我把這個函數的代碼,因爲它可以幫助你瞭解我:) 有一個好的一天

+0

你想在同一個圖表中的不同風格?或者你有多個圖表對象? –

+0

@MongZhu如果可能的話,我需要不同的風格:) –

+0

@MongZhu謝謝你的帖子:)但我不明白它是如何改變圖表的視覺?我的意思是我需要更具吸引力的圖表:) –

回答

0

您可以簡單地改變ChartType根據您的需要。如果您希望以不同樣式顯示相同的數據,則甚至不需要將新數據點綁定到Series

這裏是一個小程序來闡明這一點。它有一個綁定到ComboBox,你可以選擇樣式的SeriesChartTypes列表:

List<SeriesChartType> typeList = new List<SeriesChartType>() { 
            SeriesChartType.Pie, 
            SeriesChartType.Line, 
            SeriesChartType.Bar }; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    comboBox1.DataSource = typeList; 
} 

此外,它有一個正常的Chart對象。在組合框的SelectedIndexChanged事件中,它只是將測試數據和選擇的SeriesChartType分配給Series,並且圖表會自行重繪。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (comboBox1.SelectedIndex >= 0) 
    { 
     SeriesChartType type = typeList[comboBox1.SelectedIndex]; 

     List<int> values = new List<int> { 1, 2, 3, 3, 3, 3, 4, 5, 6, 6, 6, 4, 4, 3, 2, 2, 1, 1 }; 

     chart1.Series[0].Points.DataBindY(values); 

     chart1.Series[0].ChartType = type; 
    } 
} 
相關問題