1. はじめに
「マクロの記録」を使いながら、グラフ作成を自動化するプログラムを作りましたが、すべてをベタに書いてしまったので、再利用しにくい動作確認のためだけに使うものになってしまいました。
これから繰り返し使うことを考えて、クラスモジュールで実装しようと思います。
2. プログラムの説明
まずは、ソースコードを載せます。Class_Initializeはオブジェクトが生成されたときに実行されるSubなので、初期化をします。チャート作成に使うオブジェクトの生成と変数を初期化します。それ以外の Property Get と Property Letはペアになっていて、プロパティを作ります。クラスモジュールのソースコード
Private x_title As String
Private y_title As String
Private x_minimum_scale As Double
Private x_maximum_scale As Double
Private y_minimum_scale As Double
Private y_maximum_scale As Double
Private chart_left As Integer
Private chart_top As Integer
Private chart_width As Integer
Private chart_height As Integer
Private chart_title As String
'グラフ用のオブジェクト
Private shape_obj As Shape
Private chart_obj As Chart
Private series_obj As Series
Private series_collection_obj As SeriesCollection
Private x_axis_obj As Axis
Private y_axis_obj As Axis
Const major_unit_div As Integer = 5
Const width_heigh_ratio = 1.25
Private series_count As Integer
'初期化
Public Sub Class_Initialize()
x_title = ""
y_title = ""
chart_title = ""
'チャートの作成と必要なオブジェクトを作る
Set shape_obj = ActiveSheet.Shapes.AddChart(xlXYScatter, 0, 0, 375, 300)
Set chart_obj = shape_obj.Chart
Set series_collection_obj = chart_obj.SeriesCollection
Set series_obj = series_collection_obj.NewSeries
Set x_axis_obj = chart_obj.Axes(xlCategory)
Set y_axis_obj = chart_obj.Axes(xlValue)
chart_obj.SetElement (msoElementPrimaryCategoryGridLinesMajor)
x_axis_obj.TickLabelPosition = xlTickLabelPositionLow
y_axis_obj.TickLabelPosition = xlTickLabelPositionLow
'ひとまず、デフォルトを0~1とする。
x_minimum_scale = 0
x_maximum_scale = 1
y_minimum_scale = 0
y_maximum_scale = 1
x_axis_obj.MajorUnit = (x_maximum_scale - x_minimum_scale) / major_unit_div
y_axis_obj.MajorUnit = (y_maximum_scale - y_minimum_scale) / major_unit_div
series_count = 0
End Sub
Public Property Get chartTitle() As String
chartTitle = chart_title
End Property
Public Property Let chartTitle(chart_title_in As String)
chart_title = chart_title_in
With chart_obj
.HasTitle = True
.chartTitle.Text = chart_title
End With
End Property
Public Property Get chartLeft() As Integer
chartLeft = chart_left
End Property
Public Property Let chartLeft(chart_left_in As Integer)
chart_left = chart_left_in
shape_obj.Left = chart_left
End Property
Public Property Get chartTop() As Integer
chartTop = chart_top
End Property
Public Property Let chartTop(chart_top_in As Integer)
chart_top = chart_top_in
shape_obj.Top = chart_top
End Property
Public Property Get chartWidth() As Integer
chartWidth = chart_width
End Property
Public Property Let chartWidth(chart_width_in As Integer)
chart_width = chart_width_in
shape_obj.Width = chart_width
End Property
Public Property Get chartHeight() As Integer
chartHeight = chart_height
End Property
Public Property Let chartHeight(chart_height_in As Integer)
chart_height = chart_height_in
shape_obj.Height = chart_height
End Property
クラスモジュールを呼び出す部分のソースコードです 。グラフ作成に使うプロパティだけを実装するようにしておけば、後々使う時でも、プロパティを見ながら入力を決めていけばよいので、再利用しやすそうです。標準で用意されているオブジェクトを使って、そのままプログラムを書くと選択できるプロパティが多すぎて、いちいち記憶できません。ソフト開発を本職にしているわけではないので、なるべく楽をしたいです。このソースは、全体の一部分になっていませす。動作説明用に切り出しました。
Private Sub Class_Module_Button_Click()
Dim chart_obj As Chart_Class_Symple
Set chart_obj = New Chart_Class_Symple
With chart_obj
.chartTitle = "Chart Title"
.chartLeft = 50
.chartTop = 50
.chartHeight = 300
.chartWidth = .chartHeight * 1.25
End With
End Sub
3. クラスモジュールへ置き換え
基本ルールとしては、忘れてしまっても後で簡単に思い出せるようにプロパティを絞って実装する。また、プロパティに値を設定した時にチャートも更新する。 と、しました。実行するとこのようになります。チャートを作る位置とサイズ、チャートのタイトルをプロパティに設定するとそれを反映したグラフができます。次は、プログラム全体を紹介したいと思います。

0 件のコメント:
コメントを投稿