Top > OOoBasic > Calc > chart2 > ex6
* データ範囲の変更 [#k364ad9e]

XYチャートのデータ範囲を変更する例です。


#code(basic){{
REM  *****  BASIC  *****

Sub CreateAndSetDataForLineChart
  oDoc = ThisComponent
  'oChart = GetSelectedChart(oDoc)
  oEmbedded = oDoc.getSheets().getByIndex(0).getCharts().getByIndex(0)
  oChart = oEmbedded.getEmbeddedObject()
  
  oDataProvider = oChart.getDataProvider()
  oDiagram = oChart.getFirstDiagram()
  
  oYDataSeq = CreateLabeledSequence(_
           oDataProvider, "$Sheet1.$B$28:$B$30", "values-y")
  oCategoriesSeq = CreateLabeledSequence(_
           oDataProvider, "$Sheet1.$A$31:$A$33", "categories")
  
  oDataSource = oChart.getUsedData()
  oDataSource.setData(Array(oCategoriesSeq, oYDataSeq))
  
  sTemplateName = "com.sun.star.chart2.template.LineSymbol"
  oChartTypeManager = oChart.getChartTypeManager()
  oChartTypeTemplate = oChartTypeManager.createInstance(sTemplateName)
  
  oChartTypeTemplate.changeDiagramData(_
        oDiagram, oDataSource, Array())
End Sub


Function CreateLabeledSequence(_
  oDataProvider, _
  sDataRange, _
  sRole, _
  Optional sLabelRange)
  
  oLabeledSeq = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
  
  oYDataSequence = CreateDataSequence(oDataProvider, sDataRange, sRole)
  oLabeledSeq.setValues(oYDataSequence)
  
  If NOT IsMissing(sLabelRange) Then
    oLabelDataSequence = CreateDataSequence(oDataProvider, sLabelRange, "label")
    oLabeledSeq.setLabel(oLabelDataSequence)
  End If
  
  CreateLabeledSequence = oLabeledSeq
End Function


' creat new DataSequence from range representaion
' that provides real data and its role in the series
' oDataProvider: com.sun.star.chart2.data.XDataProvider
' sRangeRepresentation: range address e.g. Sheet1.A1:B2
' sRole: role is defined in com.sun.star.chart2.data.DataSequenceRole
Function CreateDataSequence( _
    oDataProvider As Object, _
    sRangeRepresentation As String, sRole As String ) As Object
 
  Dim oDataSequence As Object
  On Error GoTo Handler
  ' create .chart2.data.DataSequence from range representation
  If oDataProvider._
      createDataSequenceByRangeRepresentationPossible(sRangeRepresentation) Then
    oDataSequence = oDataProvider._
        createDataSequenceByRangeRepresentation(sRangeRepresentation)
    If NOT IsNull(oDataSequence) Then
      oDataSequence.Role = sRole
    End If
  End If
 
  Handler:
    CreateDataSequence = oDataSequence
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