Top > OOoBasic > Calc > chart2 > ex5

散布図の作成 Edit

以下の例では X を Sheet1.B1:B5 から、Y を Sheet1.F1:F5 として散布図を作成します。

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
Sub CreateScatterChartExample
  sChartName = "Chart100"
  oDoc = ThisComponent
  oCharts = oDoc.getSheets().getByIndex(0).getCharts()
  
  oChart = AddNewChart(oCharts, sChartName, 1000, 1000, 16000, 9000, False, True)
  
  ChangeDiagramDataAndType(oChart, _
    "com.sun.star.chart2.template.ScatterLineSymbol", _
    "Sheet1.B1:B5;Sheet1.F1:F5", _
      com.sun.star.chart.ChartDataRowSource.COLUMNS, False, False)
End Sub
 
 
Function AddNewChart(oCharts, sChartName As String, nX, nY, nWidth, nHeight, bFirstColumnAsLabel, bFirstRowAsLabel)
  Dim aRange(1) As New com.sun.star.table.CellRangeAddress
  Dim aRect As New com.sun.star.awt.Rectangle
  aRect.X = nX
  aRect.Y = nY
  aRect.Width = nWidth
  aRect.Height = nHeight
  
  oCharts.addNewByName(sChartName,aRect,aRange,bFirstColumnAsLabel,bFirstRowAsLabel)
  
  AddNewChart = oCharts.getByName(sChartName).getEmbeddedObject()
End Function
 
 
Function ChangeDiagramType(oChart, sTemplateName As String)
  oChartTypeManager = oChart.getChartTypeManager()
  oChartTypeTemplate = oChartTypeManager.createInstance(sTemplateName)
  oChartTypeTemplate.changeDiagram(oChart.getFirstDiagram())
  ChangeDiagramType = oChartTypeTemplate
End Function
 
 
Function ChangeDiagramDataAndType(oChart, sTemplateName, sCellRangeRepresentation, nDataDirection, bFirstCellAsLabel, bHasCategory)
  Dim aProps(3) As New com.sun.star.beans.PropertyValue
  aProps(0).Name = "CellRangeRepresentation"
  aProps(0).Value = sCellRangeRepresentation
  aProps(1).Name = "DataRowSource"
  aProps(1).Value = nDataDirection
  aProps(2).Name = "FirstCellAsLabel"
  aProps(2).Value = bFirstCellAsLabel
  aProps(3).Name = "HasCategories"
  aProps(3).Value = bHasCategory
  
  oDataProvider = oChart.getDataProvider()
  oDataSource = oDataProvider.createDataSource(aProps)
  
  ' change data of the diagram
  Dim aArgs(0) As New com.sun.star.beans.PropertyValue
  aArgs(0).Name = "HasCategories"
  aArgs(0).Value = bHasCategory
  
  oChartTypeTemplate = ChangeDiagramType(oChart, sTemplateName)
  oChartTypeTemplate.changeDiagramData(oChart.getFirstDiagram(), oDataSource, aArgs)
End Function

Sheet1.B1:B5;Sheet1.F1:F5 の指定を Sheet1.F1:F5;Sheet1.B1:B5 のように逆にしても X と Y は入れ替わりません。次のようにする必要があります。

ExchangeXY(oChart, 0)
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
Function ExchangeXY(oChart, nSeriesIndex)
  oDiagram = oChart.getFirstDiagram()
  oCoords = oDiagram.getCoordinateSystems()
  oCoord = oCoords(0)
  oChartTypes = oCoord.getChartTypes()
  oChartType = oChartTypes(0)
  oSeriesList = oChartType.getDataSeries()
  oSeries = oSeriesList(nSeriesIndex)
  
  oDataSequences = oSeries.getDataSequences()
  
  oValues1 = oDataSequences(0).getValues()
  oValues2 = oDataSequences(1).getValues()
  If oValues1.Role = "values-x" Then
    oValues1.Role = "values-y"
  Else
    oValues1.Role = "values-x"
  End If
  If oValues2.Role = "values-y" Then
    oValues2.Role = "values-x"
  Else
    oValues2.Role = "values-x"
  End If
  
  ' set list of data series
  oSeriesList(nSeriesIndex) = oSeries
  oChartType.setDataSeries(oSeriesList)
End Function

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes