Top > OOoBasic > Calc > chart2 > ex7

多系列散布図の作成 Edit

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

ABCD
1X1Y1X2Y2
20100.310
3111113
42132.116
5314318
6412413
75134.214
86105.815
978710
1087919
  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
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