2016-11-23 3 views
2

я сюжет сделал некоторые участки с помощью объекта Chart и SeriesCollection.NewSeries Часть кода, как этотСимметрия по отношению к оси Y в серии VBA Excel

Private Function AddSeriesAndFormats(PPSChart As Chart, shInfo As Worksheet, tests() As PPS_Test, RowCount As Integer, col As Integer, smoothLine As Boolean, lineStyle As String, transparency As Integer, lineWidth As Single, ByRef position As Integer) As Series 

Dim mySeries As Series 

Set mySeries = PPSChart.SeriesCollection.NewSeries 
With mySeries 
    .Name = tests(0).GetString() 
    .XValues = "='" & shInfo.Name & "'!R" & RowCount - UBound(tests) - 1 & "C" & CInt(4 * (col + 1) - 2) & ":R" & RowCount - 1 & "C" & CInt(4 * (col + 1) - 2) 
    .Values = "='" & shInfo.Name & "'!R" & RowCount - UBound(tests) - 1 & "C" & CInt(4 * (col + 1) - 1) & ":R" & RowCount - 1 & "C" & CInt(4 * (col + 1) - 1) 
    .Smooth = smoothLine 
    .Format.line.Weight = lineWidth 
    .Format.line.DashStyle = GetLineStyle(lineStyle) 
    .Format.line.transparency = CSng(transparency/100) 
    .MarkerStyle = SetMarkerStyle(position) 
    .MarkerSize = 9 
    .MarkerForegroundColorIndex = xlColorIndexNone 
End With 

Set AddSeriesAndFormats = mySeries 

End Function 

и PPSChart создается как этот

Private Function AddChartAndFormatting(chartName As String, chartTitle As String, integralBuffer As Integer, algoPropertyName As String) As Chart 

Dim PPSChart As Chart, mySeries As Series 

Set PPSChart = Charts.Add 
With PPSChart 
    .Name = chartName 
    .HasTitle = True 
    .chartTitle.Characters.Text = chartTitle 
    .ChartType = xlXYScatterLines 
    .Axes(xlCategory, xlPrimary).HasTitle = True 
    .Axes(xlValue, xlPrimary).HasTitle = True 
    If algoPropertyName <> "" Then 'case for Generic PPS plots 
     .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = algoPropertyName 
    Else 
     .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "PL/PR max(avg_" & integralBuffer & "ms) [mbar]" 'case for the bumper obsolate algorithm 
    End If 
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Bumper Position [mm]" 
End With 

' delete random series that might be generated 
For Each mySeries In PPSChart.SeriesCollection 
    mySeries.Delete 
Next mySeries 

Set AddChartAndFormatting = PPSChart 

End Function 

пример результата, как на картинке ниже enter image description here То, что я хочу, чтобы ось X, начиная с -350, даже то, что я не имею значения в левой части Y ось (с отрицательной стороны). Практически то, что я хочу иметь, - это ось Y в середине, даже если построенные значения положительны (симметрия между максимальным значением X и минимальным значением X в направлении оси Y). Можете ли вы сказать мне, если это возможно, и привести несколько примеров?

ответ

1

Привет, вы можете попробовать что-то вроде этого:

Dim dMinValue as Double, dMaxValue as Double 

With PPSChart 
    dMinValue = application.WorksheetFunction.Min("='" & _ 
        shInfo.Name & "'!R" & RowCount - UBound(tests) - 1 _ 
        & "C" & CInt(4 * (col + 1) - 2) & ":R" & RowCount - 1 _ 
        & "C" & CInt(4 * (col + 1) - 2)) 

    dMaxValue = application.WorksheetFunction.Max("='" & _ 
        shInfo.Name & "'!R" & RowCount - UBound(tests) - 1 _ 
        & "C" & CInt(4 * (col + 1) - 2) & ":R" & RowCount - 1 _ 
        & "C" & CInt(4 * (col + 1) - 2)) 
    .... 
    .Axes(xlValue).MinimumScale = dMinValue 
    .Axes(xlValue).MaximumScale = dMaxValue 
    .... 
End With 

Мой совет, вы должны назначить значения серии к объекту для легкого использования

Dim rSerie as Range 
Set rSerie = Range("='" & _ 
         shInfo.Name & "'!R" & RowCount - UBound(tests) - 1 _ 
         & "C" & CInt(4 * (col + 1) - 2) & ":R" & RowCount - 1 _ 
         & "C" & CInt(4 * (col + 1) - 2))) 

With .... 
    dMinValue = Application.WorksheetFunction.Min(rSerie) 
    .... 
End with 
+0

Это немного сложно, потому что сначала я создаю диаграмму, и после этого я читаю значения и создаю серию. Этот код: .Axes (xlValue) .MinimumScale = dMinValue .Axes (xlValue) .MaximumScale = dMaxValue работает только при создании диаграммы, а не при создании сериала. –

0
Sub AddChartObject() 
    Dim myChtObj As ChartObject 

    Set myChtObj = ActiveSheet.ChartObjects.Add _ 
     (Left:=100, Width:=375, Top:=75, Height:=225) 
    myChtObj.Chart.SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") 
    myChtObj.Chart.ChartType = xlXYScatterLines 

    ' delete random series that might be generated 
    For Each mySeries In myChtObj.Chart.SeriesCollection 
     mySeries.Delete 
    Next mySeries 

    ' add series 
    Set mySeries = myChtObj.Chart.SeriesCollection.NewSeries 
    mySeries.XValues = Array(0, 300) 
    mySeries.Values = Array(88, 65) 

    ' rescale axis after adding series 
    With myChtObj.Chart.Axes(xlCategory) ' otherwise can use ActiveSheet.ChartObjects(1).Chart.Axes(xlCategory) 
     .MinimumScale = -350 'ActiveSheet.Range("C33").Value 
     .MaximumScale = 350 'ActiveSheet.Range("D54").Value 
    End With 
End Sub 

symmetrical horizontal axis

Смежные вопросы