是否可以在Excel图表上绘制水平线和垂直线?

我有一些散点图的价值作为我的主要数据系列,说:

锌(ppm),X值:20,50,60,70,…

铁(ppm),Y值:30,51,10,90,…

锌和铁两者都有上限的限制,比如50ppm的铁和100ppm的锌。 我希望这些可视化显示与铁的水平线和锌的垂直线。

在辅助X轴或辅助Y轴上分别显示水平线或垂直线很容易(请参阅Peltier的博客,Stack Overflow上的数千个线程等)。 但是,同时显示两者似乎是不可能的。 例如,要显示一条垂直线,您需要指定一个新的数据序列为“直线散点图”types,将两个X值设置为100,并将两个Y值设置为0和1。

显示垂直线和水平线的根本问题似乎是,您无法拆分主轴和辅助轴之间单个数据系列的X值和Y值。 单个数据序列的X和Y值必须既在主轴上,又在次轴上。 在我的例子中引入水平线时,这会变得有问题,因为这将需要我调整二次X轴,这会影响垂直线数据系列中的100。

我目前正在通过VBA来控制我的图表,但是如果您想通过VBA或Excel正确的解决scheme,

谢谢。

@TimWilliams在这里有一个很好的答案,就是如何在图上绘制一条线 。 我想,你的问题可以重新表述为如何在图上画两条线

我按照下面的例子设置你的例子,只需select范围A2:I3并插入一个XYgraphics,而不用弄乱标签等。注意我还包括一个计算来获得XYgraphics的两行的最大值。 这是因为我想你想要阈值线尊重图的轴的最大值。

在这里输入图像说明

所以,这个代码是Tim例子的一个扩展,我们在图中引入了两个新的系列而不是一个。 对于第二行,您可以切换使用XValuesValues属性来获取x或y阈值行。

  • 对于X阈值,它位于x轴上的点50( intThresholdX )处,并且从y轴上的intThresholdX延伸。

  • 对于Y阈值,其在x轴上从0-70延伸,并且在y轴上在80( intThresholdY )处延伸。

一张图片说出了千言万语的结果:

在这里输入图像说明

码:

 Option Explicit Sub DrawTwoThresholds() Dim ws As Worksheet Dim cht As ChartObject Dim srs As Series Dim intThresholdX As Integer Dim intThresholdY As Integer Dim intMaxX As Integer Dim intMaxY As Integer Set ws = ThisWorkbook.Worksheets("data") 'switch to your worksheet Set cht = ws.ChartObjects(1) 'assumes one chart is on the sheet intThresholdX = 50 intThresholdY = 80 intMaxX = ws.Range("K2").Value intMaxY = ws.Range("K3").Value 'create x threshold line Set srs = cht.Chart.SeriesCollection.NewSeries() srs.Name = "" srs.XValues = Array(intThresholdX, intThresholdX) srs.Values = Array(intMaxY, 0) srs.MarkerStyle = xlMarkerStyleNone srs.Border.Color = vbRed 'create y threshold line Set srs = cht.Chart.SeriesCollection.NewSeries() srs.Name = "" srs.XValues = Array(0, intMaxX) srs.Values = Array(intThresholdY, intThresholdY) srs.MarkerStyle = xlMarkerStyleNone srs.Border.Color = vbRed End Sub 

感谢您的帮助罗宾! 我用你的代码的主要问题是,用户仍然必须手动收缩轴,使线看起来像他们无限扩大。

我最终设置了所有的数据序列在同一个轴上,并将X和Y的阈值定义为很高的数字(例如500000)。 之后,我通过将我的数据集的最大数量乘以1.1或用户定义的极限乘以1.1(无论哪个更大)来设置轴限制。

你的解决scheme可能更多的代码优雅,需要更less的资源,但是当涉及图表格式时,我是一个整洁的怪胎:D

 Horz(1) = 0 Horz(2) = 500000 Vert(1) = 0 Vert(2) = 500000 'First Example Data Series With ActiveChart.SeriesCollection.NewSeries .Name = ActiveSheet.Cells(1, 2) & " Max Old" .ChartType = xlXYScatterLines .AxisGroup = xlPrimary .XValues = "='Graph'!$AE$3:$AE$4" .Values = Vert .Select .Format.Line.Weight = 2.25 .Format.Line.Visible = True .Format.Line.ForeColor.RGB = RGB(195, 214, 155) 'Light Green .Format.Line.DashStyle = msoLineDash .MarkerStyle = -4142 End With 'Second Example Data Series With ActiveChart.SeriesCollection.NewSeries .Name = ActiveSheet.Cells(2, 2) & " Max Old" .ChartType = xlXYScatterLines .AxisGroup = xlPrimary .XValues = Horz .Values = "='Graph'!$AE$5:$AE$6" .Select .Format.Line.Weight = 2.25 .Format.Line.Visible = True .Format.Line.ForeColor.RGB = RGB(217, 150, 148) 'Light Red .Format.Line.DashStyle = msoLineDash .MarkerStyle = -4142 End With With ActiveChart 'Set the X axis limit .Axes(xlCategory, xlPrimary).MinimumScale = 0 .Axes(xlCategory, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(1).XValues) * 1.1, 0) 'Set the Y axis limit .Axes(xlValue, xlPrimary).MinimumScale = 0 If Application.Max(ActiveChart.SeriesCollection(1).Values) >= Application.Max(ActiveChart.SeriesCollection(5).Values) Then .Axes(xlValue, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(1).Values) * 1.1, 0) Else .Axes(xlValue, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(5).Values) * 1.1, 0) End If End With 

graphics