Top > OOoBasic > Calc > chart2 > ex5

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

#code(basic){{
Sub CreateScatterChartExample
  sChartName = "Chart100"
  oDoc = ThisComponent
  oCharts = oDoc.getSheets().getByIndex(0).getCharts()
  
  oChart = AddNewChart(oCharts, sChartName, 1000, 1000, 16000, 9000)
  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, True)
      com.sun.star.chart.ChartDataRowSource.COLUMNS, False, False)
End Sub


Function AddNewChart(oCharts, sChartName As String, nX, nY, nWidth, nHeight)
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,False,False)
  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)

#code(basic){{
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