Top > OOoBasic > Calc > chart2 > ex2

例2 Edit

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

  • チャートの作成、種類の変更、範囲を指定に関しては 例1 などを参照。
  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
' 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