【问题标题】:Display a Template MS Chart for ,NET显示 .NET 的模板 MS 图表
【发布时间】:2017-07-28 21:15:01
【问题描述】:

VB2010 使用 MS 图表控件:我认为我的问题是基本的,但还没有找到如何去做。当窗体第一次加载图表控件时,什么都不显示,甚至没有网格。当我将点加载到我的系列中时,会显示网格和点。

如何显示仅包含网格线的模板图表,以便用户可以看到要填充的图表。我确实尝试将两个虚假点添加到我的一个系列中,然后禁用该系列以不显示这些点,但图表控件不认为这是渲染网格的原因。

感谢您的帮助。

编辑:感谢@baddack 让我深思。 这是我所做的:

在表单加载时创建一个虚假系列。该系列将在应用程序的整个生命周期中保留在图表中。

        Dim srs As New Series                           'create a new series
        cht.Series.Add(srs)                             'add series to chart
        srs.ChartType = SeriesChartType.Point           'it will be a point chart with one point (or you can add several points to define your display envelope)
        srs.Name = "bogus"                              'name of our bogus series
        srs.IsVisibleInLegend = False                   'do not show the series in the legend
        srs.Points.AddXY(25000, 1000)                   'this will be a point in the upper-right corner of the envelope you want to display
        srs.Points(0).MarkerColor = Color.Transparent   'no color for the marker
        srs.Points(0).MarkerSize = 0                    'no size for the marker
        chtObstacles.Series("bogus").Enabled = True     'name of the bogus series
        chtObstacles.Update()                           'update the chart

那么当我运行我的进程时,我做的第一件事就是清除所有其他系列并启用虚假系列,以便可以使用它来调整“空”网格的大小。

        cht.Series("srs1").Points.Clear()
        cht.Series("srs2").Points.Clear()
        cht.Series("bogus").Enabled = True

然后运行为图表提供点的进程:

  if pointCount > 0 then
       'turn off the series so it will not be used in the grid sizing
       cht.Series("bogus").Enabled = False 

       'add points to the chart
       'code to add points to MS Chart
  endif

  cht.ChartAreas("chaMain").RecalculateAxesScale()  'we must recalculate the axes scale to reset the mins/maxs

  'resume updating UI
  cht.Series.ResumeUpdates()

  'force redraw of chart
  cht.Update()

【问题讨论】:

    标签: .net grid series mschart


    【解决方案1】:

    您可以在图表中添加一个空点。这将使网格显示出来,但不显示任何点。

    private void Form1_Load(object sender, EventArgs e)
    {
        chart1.Series.Clear();
        SetChartAxisLines(chart1.ChartAreas[0]);
    
        Series s = new Series();
        chart1.Series.Add(s);
        s.Points.Add();
        s.Points[0].IsEmpty = true;
    }
    
    private void SetChartAxisLines(ChartArea ca)
    {
        //X-Axis
        ca.AxisX.MajorGrid.LineColor = Color.DarkGray;
        ca.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
        ca.AxisX.MinorGrid.Enabled = true;
        ca.AxisX.MinorGrid.LineColor = System.Drawing.Color.Silver;
        ca.AxisX.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
    
        //Y-Axis
        ca.AxisY.MajorGrid.LineColor = System.Drawing.Color.DarkGray;
        ca.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
        ca.AxisY.MinorGrid.Enabled = true;
        ca.AxisY.MinorGrid.LineColor = System.Drawing.Color.Silver;
        ca.AxisY.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
    }
    

    【讨论】:

    • 谢谢。这让我知道了如何设置它,因为我想要一个具有默认 x 和 y 最小值/最大值的特定空图表。您的代码帮助我意识到我可以添加一个用于自动调整大小的虚假系列。谢谢你的提示。一会儿我会在这里写解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多