C1FlexGrid 中添加 Chart

发布时间:2013/02/05 00:02 发布者:iceman

返回博客中心

C1FlexGrid 是一款功能丰富体积轻巧的表格控件。它借鉴了微软 Excel 的操作风格,丰富的数据操作功能、快捷键、选择模式剪切板支持等。并且具有灵活的外观设置和高效的性能。

本篇文章阐述如何添加 Chart 到C1FlexGrid-Excel 的另一个强大功能。在本文中,我们将添加 C1Chart 控件,怎样设置 C1Chart 的位置。

添加 Chart 到图表中 如 Excel 中,当用户选择数据,添加图表到表单中,Chart 将直观的显示数据的走势。在 C1FlexGrid 中,我们可以通过以下代码实现:

private void addChartToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (c1FlexGrid1.Clip != "")
  {
     data = c1FlexGrid1.Clip;   // capture the selected data in the grid
     string[] values = data.Split('\r');
     if (ischart == true)         // if the chart already exists in the grid then clear the series collection
     {
       chart.ChartGroups[0].ChartData.SeriesList.Clear();
       C1.Win.C1Chart.ChartDataSeries cds;        // Add new data
       for (int i = 0; i < values.Length; i++)
       {
          if (values[ i ] != "")
          {
             cds = new C1.Win.C1Chart.ChartDataSeries();
             cds = chart.ChartGroups[0].ChartData.SeriesList.AddNewSeries();
             cds.PointData.Length = 1;
             cds.Label = "Series " + values[ i ].ToString();
             cds.Y[0] = Convert.ToInt16(values[ i ]);
          }
        }
   }
   else   // if it doesn't, then add a new chart
   {
     chart = new C1.Win.C1Chart.C1Chart();
     c1FlexGrid1.Controls.Add(chart);
     chart.Location = new Point(200, 100);
     chart.Size = new Size(300,300);
     chart.Legend.Visible = true;
     chart.Header.Visible = true;
     chart.Header.Text = "ADDED CHART CONTROL";
     chart.MouseDown += new MouseEventHandler(chart_MouseDown);
     chart.MouseCaptureChanged += new EventHandler(chart_MouseCaptureChanged);
     chart.ChartGroups[0].ChartType = C1.Win.C1Chart.Chart2DTypeEnum.Pie;
     chart.ChartGroups[0].ChartData.SeriesList.Clear();
     C1.Win.C1Chart.ChartDataSeries cds;
     for (int i = 0; i < values.Length; i++)       // Add Data
     {
        if (values[ i ] != "")
        {
          cds = new C1.Win.C1Chart.ChartDataSeries();
          cds = chart.ChartGroups[0].ChartData.SeriesList.AddNewSeries();
          cds.PointData.Length = 1;
          cds.Label = "Series " + values[ i ].ToString();
          cds.Y[0] = Convert.ToInt16(values[ i ]);
        }
      }
    }
 
   ischart = true;                                // now the chart is there on the grid
  }
}

设置 Chart 的位置:

添加 Chart 后,Chart覆盖于 C1FlexGrid 上,可以通过以下代码设置 Chart 位置。

void chart_MouseDown(object sender, MouseEventArgs e)
{
  chartPoint = e.Location;   // Capture the Chart Control for moving
}
 
void chart_MouseCaptureChanged(object sender, EventArgs e)
{
  Point tmpPoint = c1FlexGrid1.HitTest().Point;
  // drop it in the user specified place in the grid
  chart.Location = new Point(tmpPoint.X - chartPoint.X, tmpPoint.Y - chartPoint.Y);
}
效果图:

下载示例:Download CS Sample


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网