使用OWC11 畫出統計圖表 C#範例
先在設計畫面的工具列新增三個項目(三個都在COM元件內).
Microsoft Office Chart 11.0
Microsoft Office PivotTable 11.0
Microsoft Office Spreadsheet 11.0
之後拉一個Microsoft Office Chart 11.0到Form上.
一個Timer
3個RadioButton.
rdb_chChartTypeLine
rdb_chChartTypeLine3D
rdb_chChartTypeLineMarkers
以下即為程式碼原始碼.
ChartChartTypeEnum _Type;
private void Form1_Load(object sender, EventArgs e)
{
showChart(ChartChartTypeEnum.chChartTypeLine);
}
private void showChart(ChartChartTypeEnum Type)
{
try
{
axChartSpace1.Clear();
ChChart objChart = axChartSpace1.Charts.Add(0);
objChart.Type = Type;
objChart.HasLegend = true;
objChart.HasTitle = true;
objChart.Title.Caption = "數量統計";
objChart.Axes[0].HasTitle = true;
objChart.Axes[0].Title.Caption = "月份";
objChart.Axes[1].HasTitle = true;
objChart.Axes[1].Title.Caption = "個";
objChart.SeriesCollection.Add(0);
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames,
+(int)ChartSpecialDataSourcesEnum.chDataLiteral, "3C");
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,
+(int)ChartSpecialDataSourcesEnum.chDataLiteral,
"Jan" + '\t' + "Feb" + '\t' + "Mar" + '\t' + "Apr" + '\t' + "May" + '\t' + "Jun" + '\t');
Random r = new Random(DateTime.Now.Second);
string w1 = r.Next(300).ToString();
string w2 = r.Next(300).ToString();
string w3 = r.Next(300).ToString();
string w4 = r.Next(300).ToString();
string w5 = r.Next(300).ToString();
string w6 = r.Next(300).ToString();
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,
(int)ChartSpecialDataSourcesEnum.chDataLiteral,
w1 + '\t' + w2 + '\t' + w3 + '\t' + w4 + '\t' + w5 + '\t' + w6 + '\t');
objChart.SeriesCollection.Add(1);
objChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimSeriesNames,
+(int)ChartSpecialDataSourcesEnum.chDataLiteral, "Daily Needs");
objChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimCategories,
+(int)ChartSpecialDataSourcesEnum.chDataLiteral,
"Jan" + '\t' + "Feb" + '\t' + "Mar" + '\t' + "Apr" + '\t' + "May" + '\t' + "Jun" + '\t');
string b1 = r.Next(300).ToString();
string b2 = r.Next(300).ToString();
string b3 = r.Next(300).ToString();
string b4 = r.Next(300).ToString();
string b5 = r.Next(300).ToString();
string b6 = r.Next(300).ToString();
objChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues,
(int)ChartSpecialDataSourcesEnum.chDataLiteral,
b1 + '\t' + b2 + '\t' + b3 + '\t' + b4 + '\t' + b5 + '\t' + b6 + '\t');
}
catch (Exception ex)
{
timer1.Enabled = false;
MessageBox.Show(ex.Message);
}
finally
{
_Type = Type;
}
}
private void rdb_chChartTypeLine_CheckedChanged(object sender, EventArgs e)
{
if (rdb_chChartTypeLine.Checked)
{
showChart(ChartChartTypeEnum.chChartTypeLine);
return;
}
if (rdb_chChartTypeLine3D.Checked)
{
showChart(ChartChartTypeEnum.chChartTypeLine3D);
return;
}
if (rdb_chChartTypeLineMarkers.Checked)
{
showChart(ChartChartTypeEnum.chChartTypeLineMarkers);
return;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
showChart(_Type);
}