Top > OOoBasic > Calc > chart2 > ex7
* 多系列散布図の作成 [#n3b5e14c]

以下の例では二系列の XY セットを持つ散布図を作成します。
X: "Sheet2.A1:A10", Y: "Sheet2.B1:B10" と X: "Sheet2.C1:C10", Y: "Sheet2.D1:D10" です。各セル範囲の最初の行はラベルになっています。

,,A, B, C, D 
,1,X1, Y1, X2, Y2
,2,0, 10, 0.3, 10
,3,1, 11, 1, 13
,4,2, 13, 2.1, 16
,5,3, 14, 3, 18
,6,4, 12, 4, 13
,7,5, 13, 4.2, 14
,8,6, 10, 5.8, 15
,9,7, 8, 7, 10
,10,8, 7, 9, 19


#code(basic){{
Sub CreateMultiSeriesScatterChart
  chart_name = "chart3"
  chart_template = "com.sun.star.chart2.template.ScatterLineSymbol"
  ' データ範囲の指定。各要素の配列の最初の要素に X、二番目に Y のデータ範囲を指定
  data_series = Array(_
    Array("Sheet2.A1:A10", "Sheet2.B1:B10"), _
    Array("Sheet2.C1:C10", "Sheet2.D1:D10"))
  rect = make_Rectangle(500, 2500, 9000, 6000)
  COLUMNS = com.sun.star.chart.ChartDataRowSource.COLUMNS
  
  charts = ThisComponent.getSheets.getByIndex(1).getCharts
  chart = add_scatter_chart(charts, chart_name, chart_template, _
    rect, COLUMNS, false, true, data_series)
End Sub


' charts チャートコンテナ
' name チャート名
' template チャートテンプレート名
' rect チャートサイズ in com.sun.star.awt.Rectangle
' row_sourece データの配置されている方向
' as_label 最初のセルをラベルにするかどうか
' has_category カテゴリを含むかどうか
' data_ranges 配列で各データ列を指定。Array(Array(x, y))
Function add_scatter_chart(charts, name, template, rect, row_source, as_label, has_category, data_ranges)
  if charts.hasByName(name) Then
    charts.removeByName(name)
  end if
  charts.addNewByName(name, rect, _
        Array(CreateUnoStruct("com.sun.star.table.CellRangeAddress")), _
        false, false)
  chart = charts.getByName(name).getEmbeddedObject
  diagram = chart.getFirstDiagram
  data_provider = chart.getDataProvider
  color_scheme = diagram.getDefaultColorScheme
  
  chart_type_manager = chart.getChartTypeManager
  chart_type_template = chart_type_manager.createInstance(template)
  chart_type_template.changeDiagram(diagram)
  
  coords = diagram.getCoordinateSystems()
  coord = coords(0)
  chart_types = coord.getChartTypes()
  chart_type = chart_types(0)
  
  Dim series(UBound(data_ranges))
  For i = 0 To UBound(data_ranges) step 1
    r = data_ranges(i)
    s = create_scatter_series(data_provider, r(0), r(1))
    s.Color = color_scheme.getColorByIndex(i)
    series(i) = s
  Next
  
  chart_type.setDataSeries(series)
  chart_type_template.changeDiagram(diagram)' # reset
  add_scatter_chart = chart
End Function

' provider データプロバイダ
' x_repr X データ指定
' y_repr Y データ指定
Function create_scatter_series(provider, x_repr, y_repr, Optional label_repr)
  series = CreateUnoService("com.sun.star.chart2.DataSeries")
  ydata = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
  xdata = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
  
  ydata.setValues(create_data_sequence(provider, y_repr, "values-y"))
  if NOT IsMissing(label_repr) Then
    ydata.setLabel(create_data_sequence(provider, label_repr, "label"))
  end if
  xdata.setValues(create_data_sequence(provider, x_repr, "values-x"))
  series.setData(Array(ydata, xdata))
  create_scatter_series = series
End Function


' provider データプロバイダ
' repr データ指定
' role 役割
Function create_data_sequence(provider, repr, role)
  seq = nothing
  if provider.createDataSequenceByRangeRepresentationPossible(repr) Then
    seq = provider.createDataSequenceByRangeRepresentation(repr) 
    seq.Role = role
  end If
  create_data_sequence = seq
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
}}

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