Top > OOoBasic > Calc > chart2 > ex2

* 例2 [#o15f5ca6]
* 例2 [#o15f5ca6]

チャートを作成、種類を設定、範囲を指定して完成。したチャートに新しく系列を付け加えます。

- チャートの作成、種類の変更、範囲を指定に関しては [[例1>../ex1]] などを参照。

#contents

#code(basic){{
' this example makes a new XY diagram and 
' add a new series to it, additionally 
' modifies its axises.
Sub Chart_ex2
  oDoc = ThisComponent
  oCharts = oDoc.getSheets().getByIndex(0).getCharts()
  sChartName = "Chart2"
  aRect = make_Rectangle(500, 2700, 7000, 6000)
  
  oChart = AddNewChart(oCharts, sChartName, _
    "com.sun.star.chart2.template.ScatterLineSymbol", _
    aRect )
  
  oDiagram = oChart.getFirstDiagram()
  oChartTypeManager = oChart.getChartTypeManager()
  oChartTypeTemplate = oChartTypeManager.createInstance( _
      "com.sun.star.chart2.template.ScatterLineSymbol") 
  oDataProvider = oChart.getDataProvider()
  oColorScheme = oDiagram.getDefaultColorScheme()
 
  ' coordinates belongs the diagram
  oCoords = oDiagram.getCoordinateSystems()
  oCoord = oCoords(0)
 
  ' chart types belonging the coordinate
  oChartTypes = oCoord.getChartTypes()
  oChartType = oChartTypes(0)
 
  oDataSource = CreateDataSource(oDataProvider, _
    "Sheet1.A1:B6", _
    com.sun.star.chart.ChartDataRowSource.COLUMNS, _
    True, True)
 
  ' change data of the diagram
  Dim aArgs(0) As New com.sun.star.beans.PropertyValue
  aArgs(0).Name = "HasCategories"
  aArgs(0).Value = True
 
  oChartTypeTemplate.changeDiagramData(oDiagram, oDataSource, aArgs)
  
  ' add new series
  oNewSeries = CreateDataSeries_ForARange( _
      oDataProvider, "Sheet1.C2:C6", "values-y", "Sheet1.C1")
  
  ' set properties of the new series
  oNewSeries.Color = oColorScheme.getColorByIndex(1)
  aSymbol = CreateUnoStruct("com.sun.star.chart2.Symbol")
  aSymbol.Style = com.sun.star.chart2.SymbolStyle.STANDARD
  aSymbol.Size = make_Size(250, 250)
  oNewSeries.Symbol = aSymbol
  
  ' add new series to the list of data series
  oDataSeries = oChartType.getDataSeries()
  ReDim Preserve oDataSeries(1)
  oDataSeries(1) = oNewSeries
  
  ' apply new data series
  oChartType.setDataSeries(oDataSeries)
  
  ' modify axises
  oYAxis = oCoord.getAxisByDimension(1, 0)
  oXAxis = oCoord.getAxisByDimension(0, 0)
  
  SetSimpleTitile(oYAxis, "Y", True)
  SetSimpleTitile(oXAxis, "X", True)
  
  oScaleData = oXAxis.getScaleData()
  
  With oScaleData
    .Maximum = 6.0
    .Minimum = 0.0
  End With
  oXAxis.setScaleData(oScaleData)
End Sub


' create and set a simple title object for axis
' oAxis: axis of the coordinate
' sTitle: title string
' bDisplayLabels: set to show/hide its title
Function SetSimpleTitile( oAxis As Object, sTitle As String, _
    Optional bDisplayLabels As Boolean ) As Object
  oNewTitle = CreateUnoService("com.sun.star.chart2.Title")
  oNewPart = CreateUnoService("com.sun.star.chart2.FormattedString")
  oNewPart.setString(sTitle)
  oNewTitle.setText(Array(oNewPart))
  If NOT IsMissing(DisplayLabels) Then oAxis.DisplayLabels = bDisplayLabels
  oAxis.setTitleObject(oNewTitle)
End Function


' add new chart and change it type from
' oCharts: ChartContainer to make a new chart into
' sChartName: chart name
' sChartTemplateName: new type of the chart
' aRectangle: define the size and the position of the chart
Function AddNewChart( _
    oCharts As Object, sChartName As String, _
    sChartTemplateName As String, _
    aRectangle As com.sun.star.awt.Rectangle ) As Object
  
  If oCharts.hasByName(sChartName) Then
    oCharts.removeByName(sChartName)
  End If
  
  Dim aRange(1) As New com.sun.star.table.CellRangeAddress
  
  ' adds new chart
  oCharts.addNewByName(sChartName,aRectangle,aRange,False,False)
  ' get newly created chart
  oChart = oCharts.getByName(sChartName).getEmbeddedObject()
  
  ' diagram of the css.chart2
  oDiagram = oChart.getFirstDiagram()
 
  ' create template and set to it
  oChartTypeManager = oChart.getChartTypeManager()
  oChartTypeTemplate = oChartTypeManager.createInstance( _
      sChartTemplateName)
  oChartTypeTemplate.changeDiagram(oDiagram)
  
  AddNewChart = oChart
End Function


' create DataSource from arguments
' according to css.chart2.data.TabularDataProviderArguments
Function CreateDataSource( _
    oDataProvider As Object, _
    sCellRangeRepresentation As String, _
    nDataRowSource As Integer, _
    bFirstCellAsLabel As Boolean, _
    bHasCategories As Boolean ) As Object
  
  Dim oNewDataSource As Object
  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 = nDataRowSource
  aProps(2).Name = "FirstCellAsLabel"
  aProps(2).Value = bFirstCellAsLabel
  aProps(3).Name = "HasCategories"
  aProps(3).Value = bHasCategories
  
  oNewDataSource = oDataProvider.createDataSource(aProps)
  CreateDataSource = oNewDataSource
End Function


' create DataSeries represented by a single range
' oDataProvider: provided by the chart
' sYRangeRepresentation: string representation of the range like a "Sheet1.A2:A5"
' sYRole: role for the data c.g. "values-y"
' sLabelRangeRepresentation: if the data has its label, pass it
Function CreateDataSeries_ForARange( _
    oDataProvider As Object, _
    sYRangeRepresentation As String, _
    sYRole As String, _
    Optional sLabelRangeRepresentation As String ) As Object
  
  Dim oNewDataSeries As Object
  ' create new DataSeries
  oNewDataSeries = CreateUnoService("com.sun.star.chart2.DataSeries")
  
  ' Y
  oDataY = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
  oSequenceY = CreateDataSequence(oDataProvider, _
      sYRangeRepresentation, sYRole)
  If NOT IsNull(oSequenceY) Then
    oDataY.setValues(oSequenceY)
  
    If NOT ((IsMissing(sLabelRangeRepresentation)) AND _
                      (sLabelRangeRepresentation <> "")) Then
      oSequenceLabel = CreateDataSequence(oDataProvider, _
        sLabelRangeRepresentation, "")
      oDataY.setLabel(oSequenceLabel) ' label is used as name
    End If
  End If
  
  ' set x and y data to series
  aData = Array(oDataY)
  oNewDataSeries.setData(aData)
  
  CreateDataSeries_ForARange = oNewDataSeries
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


Function make_Rectangle( _
    nX As Long, nY As Long, _
    nWidth As Long, nHeight As Long ) _
        As com.sun.star.awt.Rectangle
  Dim aRectangle As New com.sun.star.awt.Rectangle
  With aRectangle
    .X = nX
    .Y = nY
    .Width = nWidth
    .Height = nHeight
  End With
  make_Rectangle = aRectangle
End Function


Function make_Size( _
    nWidth As Long, nHeight As Long ) As com.sun.star.awt.Size
  Dim aSize As New com.sun.star.awt.Size
  aSize.Width = nWidth
  aSize.Height = nHeight
  make_Size = aSize
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