在同一图表上绘制两个不同的图形

时间:2011-07-25 10:50:50

标签: c# graph charts microsoft-chart-controls

我正在使用MSChart Control绘制一些数据,但我想绘制两个不同的图 在同一张图表上。例如,使用带有饼图(系列2)的条形图(系列1)

我试图通过使用以下代码使用一个图表来显示两个不同的图形  它显示数据正确但一个问题是我想显示两个图(PIE CHART,BAR CHART)但它显示(“两个PIE图表”)

任何人都可以帮忙

NOte:我正在使用一张图表进行所有这些操作

  private void kpicartmouse_click(object sender, MouseEventArgs e)
  {
    Series acceptseries;
    Series membershiptypeseries = null;
      Title title;
    string area;

  try
  {
    var pos = e.Location;
    var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
    foreach (var result in results)
    {
      if (result.ChartElementType == ChartElementType.DataPoint)
      {


        DataTable visits = null;
        visits = KPIData.Visits(dtStartDate.Value,dtEndDate.Value, mf);

        DataTable accepts = null;
        accepts = KPIData.AcceptedvisitsByMembership(mf);

        area = "subchart";
        kpiChartControl.ChartAreas.Add(area);
        acceptseries = kpiChartControl.Series.Add(area);
        acceptseries.ChartArea = area;

        title = kpiChartControl.Titles.Add("Accepted Visits By MemberShip Type");
        title.DockedToChartArea = area;
        title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
        title.Alignment = ContentAlignment.TopLeft;
        kpiChartControl.Titles.Add("").DockedToChartArea = area;
        kpiChartControl.Titles.Add("Accepted visits by memebrship types").DockedToChartArea = area;



        foreach (Title titles in kpiChartControl.Titles)
        {
          titles.IsDockedInsideChartArea = false;
        }

        foreach (ChartArea chartArea in kpiChartControl.ChartAreas)
        {
          chartArea.Area3DStyle.Enable3D = true;
          chartArea.Area3DStyle.Inclination = 45;
          //chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview;
        }

        foreach (Series chartSeries in kpiChartControl.Series)
        {
          chartSeries.ChartType = SeriesChartType.StackedColumn;
          chartSeries["ColumnDrawingStyle"] = "SoftEdge";
          chartSeries["LabelStyle"] = "Top";
          chartSeries.IsValueShownAsLabel = true;
          chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
        }
        foreach (Series chartSeries in kpiChartControl.Series)
        {
          chartSeries.ChartType = SeriesChartType.Pie;

          chartSeries["PieLabelStyle"] = "Outside";
          chartSeries["DoughnutRadius"] = "30";
          chartSeries["PieDrawingStyle"] = "SoftEdge";

          chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
        }





        foreach (Legend legend in kpiChartControl.Legends)
        {
          legend.Enabled = false;
        }

        if (accepts == null)
        {
          acceptseries.Points.Clear();
          acceptseries.Points.AddXY("no live", 0);
        }
        if (visits == null)
        {
          membershiptypeseries.Points.Clear();
          membershiptypeseries.Points.AddXY("no membershiptypes", 0);


        }

        kpiChartControl.Series[0].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "Value");
        kpiChartControl.Series["subchart"].Points.DataBindXY(visits.Rows, "Status", visits.Rows, "Visits");



        foreach (Series chartSeries in kpiChartControl.Series)
        {
          foreach (DataPoint point in chartSeries.Points)
          {

            switch (point.AxisLabel)
            {
              case "Accepted": point.Color = Color.Green; break;
              case "Refused": point.Color = Color.Red; break;

            }
            point.Label = string.Format("{0:0}", point.YValues[0]);
          }

        }

        foreach (Series chartSeries in kpiChartControl.Series)
        {
          foreach (DataPoint point in chartSeries.Points)
          {

            switch (point.AxisLabel)
            {
              case "Silver membership": point.Color = Color.Green; break;
              case "Gold Membership": point.Color = Color.Blue; break;
              //case "Refused": point.Color = Color.Red; break;
              case "Weekend Peak": point.Color = Color.Cyan; break;
              case "prspect": point.Color = Color.Indigo; break;


            }
            point.Label = string.Format("{0:0}- {1}", point.YValues[0], point.AxisLabel);
          }

        }





      }
    }
  }
  catch
  {
  }


}

1 个答案:

答案 0 :(得分:2)

只是猜测......这段代码不会强制所有图表成为饼图吗?

    foreach (Series chartSeries in kpiChartControl.Series)
    {
      chartSeries.ChartType = SeriesChartType.Pie;

      chartSeries["PieLabelStyle"] = "Outside";
      chartSeries["DoughnutRadius"] = "30";
      chartSeries["PieDrawingStyle"] = "SoftEdge";

      chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft;
    }

如果这没有帮助,那么尝试将代码分解为更小的方法,然后依次评论每个位 - 然后您将能够理解每个位的作用。