Top > OOoBasic > Calc > chart2 > ex1
* 例1 [#z4b83470]

最も基本的なものとして
+新たなチャートを作成
+種類を変更
+新しいデータ範囲を設定
する例です。すべて chart2 モジュールを利用するようにします。

簡単な表から 3Dの円グラフを作成します。範囲は Sheet1.A1:B3 です。

&ref(chart_ex1.png,nolink);


#contents

#code(basic){{
Sub Chart_ex1
  oDoc = ThisComponent
  ' charts container of the first sheet
  oCharts = oDoc.getSheets().getByIndex(0).getCharts()
  
  Dim aRange(1) As New com.sun.star.table.CellRangeAddress
  Dim aRectangle As New com.sun.star.awt.Rectangle
  
  sChartName = "Chart2"
  aRectangle = make_Rectangle(1300, 1300, 7000, 5000)
  
  If oCharts.hasByName(sChartName) Then
    oCharts.removeByName(sChartName)
  End If
  
  ' 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( _
    "com.sun.star.chart2.template.ThreeDPie")
  oChartTypeTemplate.changeDiagram(oDiagram)
  
  ' DataProvider is used to make new data source
  oDataProvider = oChart.getDataProvider()
 
  ' coordinates belongs the diagram
  oCoords = oDiagram.getCoordinateSystems()
  oCoord = oCoords(0)
 
  ' chart types belonging the coordinate
  oChartTypes = oCoord.getChartTypes()
  oChartType = oChartTypes(0)
 
  ' create new datasource from arguments
  ' valid names of arguments are defined in
  ' css.chart2.data.TabularDataProviderArguments service
  Dim aProps(3) As New com.sun.star.beans.PropertyValue
  aProps(0).Name = "CellRangeRepresentation"
  aProps(0).Value = "Sheet1.A1:B3"
  aProps(1).Name = "DataRowSource"
  aProps(1).Value = com.sun.star.chart.ChartDataRowSource.COLUMNS
  aProps(2).Name = "FirstCellAsLabel"
  aProps(2).Value = False
  aProps(3).Name = "HasCategories"
  aProps(3).Value = True
 
  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 = True
 
  oChartTypeTemplate.changeDiagramData(oDiagram, oDataSource, aArgs)
End Sub


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
}}

表示されるチャートは色が手動で作成したものと少し違いますが、これは 3D データの違いによるものです。以下の様にすると同じになります。
  With oDiagram
    .D3DSceneAmbientColor = 13421772
    .D3DSceneLightColor2 = 3355443
    .D3DSceneShadowSlant = 0
  End With

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