在DataVisualization.Charting.Chart的两个Y轴中对齐零

时间:2014-01-27 22:32:44

标签: vb.net charts axis data-visualization

我在DataVisualization.Charting.Chart和两个AxisY(AxisY1和AxisY2)中有两个系列。我能够在图表上绘制两个系列,但左轴和右轴的零点未对齐。如何对齐两轴的零点?

NetChart.Series.Clear()
NetChart.Series.Add("Net")
NetChart.Series.Add("Net Cum.")
NetChart.Series(0).Points.Clear()
NetChart.Series(1).Points.Clear()

Dim netSaleDT As DataTable = SomeDataTable
netSaleDT.Columns.Add("CumulativeNet", GetType(Decimal))
Dim index As Integer = 0
Dim cumulSum As Decimal = 0
For Each drow As DataRow In netSaleDT.Rows
     cumulSum += Convert.ToDecimal(drow("Net"))
     drow("CumulativeNet") = cumulSum
     NetChart.Series(0).Points.AddXY(drow("myMonth"), Convert.ToDecimal(drow("Net")))
     NetChart.Series(1).Points.AddXY(drow("myMonth"), Convert.ToDecimal(drow("CumulativeNet")))
     NetChart.Series(0).Points(index).AxisLabel = drow("myMonth")
     index += 1
Next

NetChart.Series(0).YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Primary
NetChart.Series(1).YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary
NetChart.Series(1).ChartType = DataVisualization.Charting.SeriesChartType.Line
NetChart.ChartAreas(0).AxisX.Interval = 2
NetChart.ChartAreas(0).AxisY.LabelStyle.Format = "C"
NetChart.ChartAreas(0).AxisY2.LabelStyle.Format = "C"

NetChart.Series(1).BorderWidth = 2
NetChart.Series(1).Color = Color.Blue
NetChart.ChartAreas(0).AxisY2.Enabled = DataVisualization.Charting.AxisEnabled.True
NetChart.ChartAreas(0).AxisY2.LabelStyle.Enabled = True

1 个答案:

答案 0 :(得分:0)

轴属性是否设置为自动?

如果是,则需要以编程方式手动设置每个轴。

http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.axis.minimum%28v=vs.110%29.aspx

当我设定最小值/最大值时,我通常会这样做:

        Dim myChart As Excel.ChartObject = CType(xlCharts.Item(y), ChartObject)
        Dim chartPage As Excel.Chart = myChart.Chart
        chartPage.Axes(Excel.XlAxisType.xlCategory).minimumscale = minimum
        chartPage.Axes(Excel.XlAxisType.xlCategory).maximumscale = maximum

这会更改x轴刻度,以更改Y刻度:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chartarea.axisy%28v=vs.110%29.aspx

并更改次要y比例:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chartarea.axisy2%28v=vs.110%29.aspx

希望这有帮助

*编辑

好吧,我试图不给你完整的答案

但我想:

    Dim myChart As Excel.ChartObject = CType(QoE.xLSheet1.ChartObjects(0), ChartObject)
    Dim chartPage As Excel.Chart = myChart.Chart
    chartPage.Axes(Excel.XlAxisGroup.xlPrimary).minimumscale = 0
    chartPage.Axes(Excel.XlAxisGroup.xlSecondary).minimumscale = 0

将图表对象设置为您尝试更改的任何图表,然后将主要和次要y轴都设置为0