Class: ExcelWps::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_wps.rb

Overview

图表

Constant Summary collapse

ColumnClustered =

图形类型

51
ColumnStacked =

簇状柱形图

52
Doughnut =

堆积柱状图

-4120           # 圆环图
Line =

圆环图

4
Pie =

折线图

5
BarClustered =

饼图

57
DataLabelsShowLabel =

标签数据

4
DataLabelsShowLabelAndPercent =

数据点所属的分类

5
DataLabelsShowPercent =

占比百分比以及所属分类,仅饼图

3
DataLabelsShowValue =

百分比, 仅饼图

2

Instance Method Summary collapse

Constructor Details

#initialize(active) ⇒ Chart

Returns a new instance of Chart.



330
331
332
# File 'lib/excel_wps.rb', line 330

def initialize(active)
  @chart = active
end

Instance Method Details

#axes_unit(int) ⇒ Object

设置坐标轴格式, 刻度单位



377
378
379
380
# File 'lib/excel_wps.rb', line 377

def axes_unit(int)
  @chart.Axes(2).MajorUnit = int
  @chart.Axes(2).MinorUnit = int
end

#axes_x=(name) ⇒ Object

设置X轴名称, 只用于条形图



365
366
367
368
# File 'lib/excel_wps.rb', line 365

def axes_x=(name)
  @chart.Axes(1,1).HasTitle = true
  @chart.Axes(1,1).AxisTitle.Characters.Text = name
end

#axes_y=(name) ⇒ Object

设置Y轴名称, 只用于条形图



371
372
373
374
# File 'lib/excel_wps.rb', line 371

def axes_y=(name)
  @chart.Axes(2,1).HasTitle = true
  @chart.Axes(2,1).AxisTitle.Characters.Text = name
end

#category_name=(bool) ⇒ Object

标签类别名称开关



414
415
416
417
418
# File 'lib/excel_wps.rb', line 414

def category_name=(bool)
  now_table do |n|
    n.ShowCategoryName = bool
  end
end

#chart_workObject



334
335
336
# File 'lib/excel_wps.rb', line 334

def chart_work
  @chart
end

#data_label(type = DataLabelsShowLabelAndPercent) ⇒ Object

添加饼图的百分比



392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/excel_wps.rb', line 392

def data_label(type=DataLabelsShowLabelAndPercent)

  # 应用标签选项
  @chart.ApplyDataLabels(type)

  # 取消标签选项的系列名称
  now = @chart.SeriesCollection(1).DataLabels
  now.ShowSeriesName = false

  # 将图例放到右边
  now = @chart.Legend
  now.Position = -4152
end

#now_tableObject

标签选项



339
340
341
342
343
344
345
346
# File 'lib/excel_wps.rb', line 339

def now_table
  #@chart.SeriesCollection(n).DataLabels
  num = @chart.SeriesCollection.Count
  num.times do |t|
    t += 1
    yield @chart.SeriesCollection(t).DataLabels
  end
end

#series_name=(bool) ⇒ Object

标签系列名称开关



407
408
409
410
411
# File 'lib/excel_wps.rb', line 407

def series_name=(bool)
  now_table do |n|
    n.ShowSeriesName = bool
  end
end

#source=(range) ⇒ Object

这是原数据地址, 以每列数据作为一个系列



355
356
357
# File 'lib/excel_wps.rb', line 355

def source=(range)
  @chart.SetSourceData(range, 2)
end

#style=(int) ⇒ Object

修改样式通过录制宏可以查看样式编号条形图中203 比较好看饼图中 251, 254 比较好看



386
387
388
389
# File 'lib/excel_wps.rb', line 386

def style=(int)
  @chart.ChartStyle = int
  data_label
end

#title=(name) ⇒ Object

修改标题



349
350
351
352
# File 'lib/excel_wps.rb', line 349

def title=(name)
  @chart.HasTitle = true
  @chart.ChartTitle.Characters.Text = name
end

#type=(c_type) ⇒ Object

更改图形类型



360
361
362
# File 'lib/excel_wps.rb', line 360

def type=(c_type)
  @chart.ChartType = c_type
end

#value=(bool) ⇒ Object

标签值开关



421
422
423
424
425
426
427
# File 'lib/excel_wps.rb', line 421

def value=(bool)
  now_table do |n|
    n.ShowValue = bool
    n.ShowLegendKey = 0 unless bool
    n.Separator = " " unless bool
  end
end