- 日時: 2012/07/04 14:01
- 名前: VBレスキュー(花ちゃん)
- ***********************************************************************************
* カテゴリー:[チャート][描画・画像][] * * キーワード:Microsoft Chart Controls,MSChart,行/列切り替え,系列入れ替え,行と列 * *********************************************************************************** '=================================================================================================== '投 稿 日:2012.07.04 '投 稿 者:VBレスキュー(花ちゃん) 'タイトル:Chartコントロール(VB2008〜版)で行と列を入れ替えて表示 '========1=========2=========3=========4=========5=========6=========7=========8=========9=========0 動作確認:WindowsVista / Windows 7 / Excel 2007/2010 VB2010 / Framework 4 / ターゲットCPU:X86 [Option Compare Text] [Option Explicit On] [Option Infer On] [Option Strict On]で設定 '---------------------------------------------------------------------------------------------------
'チャート関係の名前空間です。 Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Private frgRowCol As Boolean Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim ds As New DataSet Using cn As New System.Data.OleDb.OleDbConnection Dim csvFileName As String = "Chart.csv" ' fileName cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "..\..\..\data\" & _ ";Extended Properties=""Text;HDR=YES;IMEX=1;FMT=Delimited""" Using da As System.Data.OleDb.OleDbDataAdapter = _ New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & "Chart.csv", cn) da.Fill(ds, "Table1") Chart1.DataSource = ds End Using End Using
frgRowCol = Not frgRowCol 'クリック毎に行と列を入れ替えるフラグ Chart1.Series.Clear() '系列(データ関係)の初期化
If frgRowCol Then '通常の読み込み表示 For i As Integer = 1 To ds.Tables(0).Columns.Count - 1 Dim columnName As String = ds.Tables(0).Columns(i).ColumnName.ToString() Chart1.Series.Add(columnName) Chart1.Series(columnName).ChartType = DataVisualization.Charting.SeriesChartType.Column Chart1.Series(columnName).XValueMember = ds.Tables(0).Columns(0).ColumnName.ToString() Chart1.Series(columnName).YValueMembers = columnName Next i Else '行と列を入れ替えて読み込み表示 For i As Integer = 0 To ds.Tables(0).Rows.Count - 1 '浅野・安室 等の氏名を系列名に設定 'Debug.Print(Chart1.Series(i).Name) Dim columnName As String = ds.Tables(0).Rows(i).Item(0).ToString() Chart1.Series.Add(columnName) '系列毎にグラフの種類を設定 Chart1.Series(columnName).ChartType = SeriesChartType.Column For j As Integer = 1 To (ds.Tables(0).Columns.Count) - 1 Dim YVal As String = CStr(ds.Tables(0).Rows(i).Item(ds.Tables(0).Columns(j).ColumnName)) 'Debug.Print(Chart1.Series(columnName).Name & " " & ds.Tables(0).Columns(j).ColumnName & " : " & YVal) '氏名毎に、科目名と点数を個別にコレクションの末尾に追加(浅野= 国語,69,数学, 81,英語,73) Chart1.Series(columnName).Points.AddXY(ds.Tables(0).Columns(j).ColumnName, YVal) Next j Next i 'Debug.Print(Chart1.Series(0).Points.Item(0).AxisLabel.ToString) 'Debug.Print(Chart1.Series(0).Points.Item(0).YValues(0).ToString) End If
'正しく入れ替っているか?確認の為に表示 '■------------------- データポイントラベルの設定・表示 ----------------Start--■ For i As Integer = 0 To Chart1.Series.Count - 1 With Chart1.Series(i) 'データラベルにデータポイントの Y 値を表示 .Label = "#VALY" 'マクロ(キーワード)で指定 注@ 参照 .LabelForeColor = Color.Blue .Font = New Font("MS P明朝", 10, FontStyle.Regular) End With Next '■---------------------------------------------------------------------End----■ End Sub
End Class
|