【问题标题】:Unexpected Behavior from .Net Charts.Net 图表中的意外行为
【发布时间】:2017-06-08 05:24:43
【问题描述】:

我正在尝试创建 .Net 图表 (Windows.Forms) 并将其导出到 .jpeg 文件。

下面是它的代码

public void ExportRouteGraphToJpeg(string nameOfChart, int xMax, int yMax, int xMin, int yMin )
{
    var chartForExport = new Chart();
    chartForExport.Series.Clear();

    var xVsYSeries = new Series
    {
        Name = "XvsY",
        Color = Color.Blue,
        IsVisibleInLegend = false,
        IsXValueIndexed = true,
        ChartType = SeriesChartType.Point,
        MarkerSize = 6, 
        MarkerColor   = Color.Blue 
    };

    for (var i = 0; i < xValue.Length; i++)
    {
        xVsYSeries.Points.AddXY(xValue[i], yValue[i]);
    }

    chartForExport.Series.Add(xVsYSeries);

    // Chart Area Definition
    var chartArea = new ChartArea
    {
        Name = "SupportPoints",
        AxisX =
        {
            Title = "X Co-ordinate [cm]",
            ArrowStyle = AxisArrowStyle.Triangle,
            Maximum = xMax + 100,
            Minimum = xMin - 100
        },
        AxisY =
        {
            Title = "Y Co-ordinate [cm]",
            ArrowStyle = AxisArrowStyle.Triangle,
            Maximum = yMax + 100,
            Minimum = yMin - 100
        }
    };

    chartForExport.ChartAreas.Add(chartArea);
    chartForExport.Location = new Point(0, 50);
    chartForExport.TabIndex = 0;

    chartForExport.Name = "ChartRoute";          

    chartForExport.SaveImage(nameOfChart + ".jpeg", ChartImageFormat.Jpeg);
}

我得到的输出如下所示(xValue 的 5 个点 = 1000、2000、3000、4000 和 yValue 相同)

当我为AxisX and AxisY 删除Maximum = yMax + 100, Minimum = yMin - 100 时,我得到了所需的输出。请看下面

任何建议为什么会发生这种情况?

【问题讨论】:

  • 第一个输出是否只有在保存到 jpg 文件或显示在表单上时才会发生?
  • @LeiYang 我不使用 Form,我只是将 Form 程序集用于控制台应用程序。
  • 你能提供一个工作样本吗? xValueyValue不要编译,我想复制试试看。
  • @LeiYang,请找到值` xValue = new[] {1000, 2000, 3000, 4000, 5000}; yValue = new[] { 1000, 2000, 3000, 4000, 5000 }; xMax = xValue.Max(); yMax = yValue.Max(); xMin = xValue.Min(); yMin = yValue.Min();`
  • 我注意到默认 MaximumMinimum 都是 NaN

标签: c# .net winforms charts mschart


【解决方案1】:

从您的系列定义中删除 IsXValueIndexed = true

【讨论】:

  • 嗯,问题不在于它为什么有效,而在于它为什么会打破图表。如果您查看MSDN - IsXValueIndexed 属性文档,它会告诉您何时将其设置为 true,它会忽略点 X 值并使用其索引。此外,如果您手动将 Minimum 设置为 0(零),您将看到所有点 "Stacked",接近 X = 0。
  • 底线是:如果您要使用某些属性,您应该知道为什么要使用以及它如何影响图表。 Chart 对象在属性中拥有无数的属性,很容易在你的脸上炸掉……:O)
【解决方案2】:

使用一些第三方图表,例如高位图表,示例如下

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Bar Chart</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
      <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>

      <script type="text/javascript">
          $(function() {
              $('#container').highcharts({
                  chart: {
                      type: 'bar'
                  },
                  title: {
                      text: 'MyFruit database info'
                  },
                  xAxis: {
                      categories: ['Records', 'Fragmentation', 'Users']
                  },
                  yAxis: {
                      title: {
                          text: 'Values'
                      }
                  },
                  series: [
                      {
                          name: 'MyFruit_Sites',
                          data: [10, 50, 4]
                      }, {
                          name: 'MyFruit_Devices',
                          data: [500, 80, 3]
                      }
                  ]
              });

              // the button handler
              $('#button').click(function () {
                  var chart = $('#container').highcharts();
                  chart.addSeries({
                              name: 'MyFruit_Users',
                              data: [30, 60, 4]
                          });
                });
          });
      </script>
  </head>
  <body>
    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
      <button id="button" class="autocompare">Add series</button>
  </body>
</html>

【讨论】:

  • 这不是答案,也不是在c#中
  • 对不起,你在这里没有帮助。我在问一个问题,但你回答的是你知道这不是问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 2021-10-02
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多