Microsoft.Office.Interop.PowerPoint图表 - 断开y轴

时间:2014-06-24 05:57:00

标签: winforms powerpoint winforms-interop

我们使用Interop PowerPoint Chart类型生成区域图,如图所示。我们需要“破碎的”y轴。我们不希望在栏上打破 - 我们只需要在y轴上休息。但无法找到实现此目的的属性或方法。

这是一个winforms应用程序。非常感谢一些指点... Chart with broken y-axis

1 个答案:

答案 0 :(得分:0)

偶然发现了这个问题,我让好奇心接管了。 我从this site获得了它的完成方式。

我使用上面网站的示例数据进行本演练。

步骤大致如下:

  1. 为数据分离指定剪切值。使用剪切值分隔数据(我使用剪切值7,500,000作为示例)
  2. 这是与示例的数据分离(上面是原始的,下面是分开的):

                    May         June        July
        London      1,234,565   1,452,369   1,478,852
        Paris       2,363,645   34,568,876  5,562,413
        Madrid      32,645,254  3,211,654   5,857,421
        Brussels    5,914,753   5,544,221   3,620,015
        Lisbon      5,824,676   4,541,258   4,015,876
        Munich      2,501,478   6,325,698   4,569,872
    
                    May         June        July        Column4     Column5     Column6
        London      1,234,565   1,452,369   1,478,852   0           0           0
        Paris       2,363,645   7,500,000   5,562,413   0           34,568,876  0
        Madrid      7,500,000   3,211,654   5,857,421   32,645,254  0           0
        Brussels    5,914,753   5,544,221   3,620,015   0           0           0
        Lisbon      5,824,676   4,541,258   4,015,876   0           0           0
        Munich      2,501,478   6,325,698   4,569,872   0           0           0
    
    1. Column4Column5Column6分配到辅助轴。

      Dim c As Microsoft.Office.Interop.PowerPoint.Chart
      Dim sc As Microsoft.Office.Interop.PowerPoint.SeriesCollection = Nothing
      Dim sr As Microsoft.Office.Interop.PowerPoint.Series = Nothing
      
      sc = c.SeriesCollection
      For i = 4 To sc.Count
          sr = sc.SeriesCollection(i)
          sr.AxisGroup = Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlSecondary
      Next
      
    2. 更改主轴和辅助轴刻度以适合图表分离。我将主轴刻度从0更改为1.6e7,将辅轴刻度从-7.0e7更改为7.0e7。同时,以百万为单位更改显示单位,并删除所有网格线。

      Dim ax As Microsoft.Office.Interop.PowerPoint.Axes
      Dim axpri As Microsoft.Office.Interop.PowerPoint.Axis
      Dim axsec As Microsoft.Office.Interop.PowerPoint.Axis
      
      ax = c.Axes
      axpri = ax.Item(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, _
                      Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary)
      axsec = ax.Item(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, _
                      Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlSecondary)
      axpri.MinimumScale = 0
      axpri.MaximumScale = 1.6e7
      axpri.DisplayUnit = Microsoft.Office.Interop.PowerPoint.XlDisplayUnit.xlMillions
      axpri.HasMajorGridlines = False
      axpri.HasMinorGridlines = False
      
      axsec.MinimumScale = -7.0e7
      axsec.MaximumScale = 7.0e7
      axsec.DisplayUnit = Microsoft.Office.Interop.PowerPoint.XlDisplayUnit.xlMillions
      axsec.HasMajorGridlines = False
      axsec.HasMinorGridlines = False
      
    3. 更改主轴和辅助轴编号格式,以便每个轴仅显示其自己的预期值。不会显示超过8M的主轴,并且不会显示小于30M的辅助轴。

      axpri.TickLabels.NumberFormat = "[<=8]0;;;"
      axsec.TickLabels.NumberFormat = "[>=30]0;;;"
      
    4. 在辅助轴中重新着色系列以匹配主轴中的系列。

      Dim srPrev As Microsoft.Office.Interop.PowerPoint.Series = Nothing
      
      For i = 4 To sc.Count
          sr = sc.SeriesCollection(i)
          srPrev = sc.SeriesCollection(i - 3)
          sr.Format.Fill.ForeColor.RGB = srPrev.Format.Fill.ForeColor.RGB
      Next
      
    5. 删除Column4Column5Column6的图例,以获得无缝的图表图例。

      c.Legend.LegendEntries(4).Delete()
      c.Legend.LegendEntries(5).Delete()
      c.Legend.LegendEntries(6).Delete()
      
    6. [可选]为超过剪切值的数据点添加整齐的颜色渐变。

      Dim p As Microsoft.Office.Interop.PowerPoint.Point = Nothing
      
      p = c.SeriesCollection("June").Points("Paris")
      p.Format.Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1)
      p.Format.Fill.GradientAngle = 270
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.ForeColor.RGB, 0.8)
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.BackColor.RGB, 0.97)
      
      p = c.SeriesCollection("May").Points("Madrid")
      p.Format.Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1)
      p.Format.Fill.GradientAngle = 270
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.ForeColor.RGB, 0.8)
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.BackColor.RGB, 0.97)
      
      p = c.SeriesCollection("Column5").Points("Paris")
      p.Format.Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1)
      p.Format.Fill.GradientAngle = 90
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.ForeColor.RGB, 0.7)
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.BackColor.RGB, 0.95)
      
      p = c.SeriesCollection("Column4").Points("Madrid")
      p.Format.Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1)
      p.Format.Fill.GradientAngle = 90
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.ForeColor.RGB, 0.7)
      p.Format.Fill.GradientStops.Insert(p.Format.Fill.BackColor.RGB, 0.95)
      
    7. 瞧!努力工作得到回报,PowerPoint图表中的y轴断了。

    8. enter image description here