※サンプルコードに”System.IO”の参照が抜けていたので追加しました。
当クラスをプロジェクトに追加してください。
ダウンロードしたdllへの参照を設定するか、ソースファイルを追加してください。
当クラス群は、下図の階層構造になっています。
まずは、グラフ描画クラスのインスタンスを作成します。
このインスタンスが階層のルートです。
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Dustin.Utils;
using Dustin.Drawing;
using Dustin.Graph;
IGraphPainter graph = DusGraph.CreateGraph( 600, 400 );
グラフのタイトルを設定します。
graph.Title.Text = "テスト グラフ";
graph.Title.Font = new Font( graph.Title.Font.FontFamily, 12, FontStyle.Bold );
graph.Title.Brush = Brushes.White;
graph.Title.BackBrush = Brushes.DarkGray;
graph.Title.Size = new SizeF( 140, 30 );
graph.Title.Location = new PointF( graph.Width / 2.0f - graph.Title.Size.Width / 2.0f, 5f );
graph.Title.StringFormat.Alignment = StringAlignment.Center;
graph.Title.StringFormat.LineAlignment = StringAlignment.Center;
graph.Title.Border.Visible = true;
graph.Title.Border.Pen = new Pen( Color.Purple, 2 );
graph.Title.CornerRadius = 8;
graph.Title.Shadow.Visible = true;
graph.Title.Shadow.Width = 5;
graph.Title.Visible = true;
グラフの背景、マージンを設定します。
graph.BackBrush = Brushes.Gray;
graph.Border.Pen = Pens.Gray;
graph.PlotArea.BackBrush = Brushes.White;
graph.PlotArea.Border.Pen = Pens.Black;
graph.PlotArea.Margin.Left = 40;
graph.PlotArea.Margin.Right = 20;
graph.PlotArea.Margin.Top = 20;
graph.PlotArea.Margin.Bottom = 40;
graph.SmoothingMode = SmoothingMode.AntiAlias;
Y軸を設定します。
IAxis axis;
axis = graph.YAxis;
axis = graph.Axis.Left;
axis.Scale.Min = 0;
axis.Scale.Max = 12;
axis.Ticks.Labels.LabelFormat = "0.0";
axis.Ticks.Labels.Interval = 2;
axis.Ticks.Major.LineLength = 5;
axis.Ticks.Major.Pen = new Pen( Color.Red, 1 );
axis.Ticks.Major.Interval = 1;
axis.Ticks.Major.SuppressFirst = true;
axis.Ticks.Major.SuppressLast = true;
axis.Ticks.Side = DusGraph.eTickSide.Left;
axis.Ticks.Direction = DusGraph.eTickDirection.Inside;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1.0;
axis.Grid.Major.SuppressFirst = true;
axis.Grid.Major.SuppressLast = true;
axis.Grid.Major.Pen = new Pen( Color.FromArgb(100, 100, 100), 1 );
axis.Grid.Major.Pen.DashPattern = new float[] { 4, 3 };
axis.Grid.Minor.Visible = true;
axis.Grid.Minor.Interval = 0.5;
axis.Grid.Minor.SuppressFirst = true;
axis.Grid.Minor.SuppressLast = true;
axis.Grid.Minor.Pen = Pens.LightGray;
同様にX軸を設定します。
axis = graph.XAxis;
axis = graph.Axis.Bottom;
axis.Scale.Min = 0;
axis.Scale.Max = 10;
axis.Ticks.Labels.LabelFormat = "0.0";
axis.Ticks.Labels.Interval = 2;
系統データを作成します。
ISeriesXYPlot sr;
sr = graph.CreateSeries( DusGraph.ePlotType.Line ).asXYPlot;
sr.YAxis = graph.Axis.Left;
sr.Mark.Visible = true;
sr.Mark.Type = DusGraph.ePlotMarkType.Circle;
sr.Mark.Brush = new SolidBrush( Color.FromArgb( 255, 200, 200 ) );
sr.asLine.Pen = new Pen( Color.FromArgb( 180, 128, 10, 10 ), 2 );
sr.asLine.Pen.DashStyle = DashStyle.Dash;
sr.Data.Add( new ST_PlotPoint( 0, 0.3 ) );
sr.Data.Add( new ST_PlotPoint( 1, 1.0 ) );
sr.Data.Add( new ST_PlotPoint( 2, 2.4 ) );
sr.Data.Add( new ST_PlotPoint( 3, 4.0 ) );
graph.Series.Add( sr );
必要な設定が終わったら、内部イメージバッファへのグラフ描画を指示します。
graph.Draw();
内部イメージバッファへのアクセスは下記のようにします。
using( Graphics g = graph.CreateGraphics() ) {
g.DrawLine( Pens.Red, 10, 10, 20, 20 );
}
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
graph.Image.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
最後にリソースの破棄を指示します。
graph.Dispose();
ページ上部へ
下図のグラフを描画するサンプルコードを掲載します(ASP.NET)。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="Dustin.Utils" %>
<%@ Import Namespace="Dustin.Drawing" %>
<%@ Import Namespace="Dustin.Graph" %>
<script runat="server">
</script>
<%
IGraphPainter graph = DusGraph.CreateGraph(300, 200);
graph.PlotArea.Margin.Left = 30;
graph.PlotArea.Margin.Right = 10;
graph.PlotArea.Margin.Top = 10;
graph.PlotArea.Margin.Bottom = 20;
IAxis axis;
axis = graph.YAxis;
axis.Scale.Min = -3;
axis.Scale.Max = 10;
axis.Scale.Base= 0;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
axis = graph.XAxis;
axis.Scale.Min = 0;
axis.Scale.Max = 5;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
ISeriesXYPlot sr;
sr = graph.CreateSeries( DusGraph.ePlotType.Bar ).asXYPlot;
sr.Data.Add( new ST_PlotPoint(0, 5) );
sr.Data.Add( new ST_PlotPoint(1, -2) );
sr.Data.Add( new ST_PlotPoint(2, 8) );
sr.Data.Add( new ST_PlotPoint(3, 6) );
sr.Data.Add( new ST_PlotPoint(4, -1.2) );
sr.Data.Add( new ST_PlotPoint(5, 5.5) );
sr.asBar.Brush = Brushes.Chocolate;
sr.asBar.Border.Pen = Pens.Brown;
sr.asBar.BarWidth = 8;
graph.Series.Add( sr );
sr = graph.CreateSeries( DusGraph.ePlotType.Line ).asXYPlot;
sr.Data.Add( new ST_PlotPoint(0, 1) );
sr.Data.Add( new ST_PlotPoint(1, 0.5) );
sr.Data.Add( new ST_PlotPoint(2, 2) );
sr.Data.Add( new ST_PlotPoint(3, 2.5) );
sr.Data.Add( new ST_PlotPoint(4, 3.6) );
sr.Data.Add( new ST_PlotPoint(5, 4) );
sr.Mark.Visible = true;
sr.Mark.Type = DusGraph.ePlotMarkType.Star;
sr.Mark.Brush = Brushes.LightBlue;
sr.Mark.Width = 10;
sr.Mark.Height = 10;
sr.Mark.Border.Pen = Pens.Blue;
sr.asLine.Pen = Pens.Blue;
graph.Series.Add( sr );
ITickLabels labels = graph.Axis.Bottom.Ticks.Labels;
System.Collections.Generic.IList<ST_TickLabelPoint> labelPoints = labels.Points;
for( int i = 0; i <= 10; i++ ) {
labelPoints.Add( new ST_TickLabelPoint((double)i, i.ToString("00") + ".") );
}
graph.Draw();
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
graph.Image.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
graph.Dispose();
%>
ページ上部へ
下図のグラフを描画するサンプルコードを掲載します(ASP.NET)。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="Dustin.Utils" %>
<%@ Import Namespace="Dustin.Drawing" %>
<%@ Import Namespace="Dustin.Graph" %>
<script runat="server">
</script>
<%
IGraphPainter graph = DusGraph.CreateGraph(280, 200);
graph.PlotArea.Margin.Left = 30;
graph.PlotArea.Margin.Right =100;
graph.PlotArea.Margin.Top = 10;
graph.PlotArea.Margin.Bottom = 20;
graph.PlotArea.InnerMargin.Left = 20;
graph.PlotArea.InnerMargin.Right = 20;
graph.Legend.Visible = true;
graph.Legend.Location = new PointF( 190, 5 );
graph.Legend.Size = new SizeF( 80, 80 );
IAxis axis;
axis = graph.YAxis;
axis.Scale.Min = 0;
axis.Scale.Max = 7;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
axis = graph.XAxis;
axis.Scale.Min = 0;
axis.Scale.Max = 2;
ISeriesStackedBarPlot sr;
sr = graph.CreateSeries( DusGraph.ePlotType.StackedBar ).asStackedBar;
sr.Data.Add( new TStackedBarPoint(0, 0, new double[] {1, 2, 3}) );
sr.Data.Add( new TStackedBarPoint(1, 0, new double[] {0.5, 2, 0.5}) );
sr.Data.Add( new TStackedBarPoint(2, 0, new double[] {2, 0.5, 2}) );
sr.Brushes = new Brush[] { Brushes.Pink, Brushes.SkyBlue, Brushes.Chocolate };
sr.Titles = new String[] { "ピンク", "ブルー", "チョコ" };
sr.Pen = new Pen( Color.Brown, 1 );
sr.Pen.DashPattern = new float[] { 4, 3 };
sr.Border.Pen = Pens.Brown;
sr.BarWidth = 12;
graph.Series.Add( sr );
graph.Draw();
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
graph.Image.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
graph.Dispose();
%>
ページ上部へ
下図のグラフを描画するサンプルコードを掲載します(ASP.NET)。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="Dustin.Drawing" %>
<%@ Import Namespace="Dustin.Chart.PieChart" %>
<script runat="server">
</script>
<%
using( Bitmap img = new Bitmap(250, 140) )
using( Graphics g = Graphics.FromImage(img) ) {
g.Clear( Color.White );
TPieChart pie = new TPieChart();
pie.Size = new SizeF( 120, 120 );
pie.StartAngle = -90f;
pie.Data.Add( new TPieData(50f, "ピンク", Brushes.Pink) );
pie.Data.Add( new TPieData(20f, "グリーン", Brushes.LightGreen) );
pie.Data.Add( new TPieData(30f, "チョコ", Brushes.Chocolate) );
TPieData pieData = new TPieData( 20f, "マゼンタ", Brushes.Magenta );
pieData.OffsetR = 11;
pie.Data.Add( pieData );
pie.Draw( g, new PointF(20, 15) );
TLegendBox legend = new TLegendBox();
legend.Size = new SizeF( 80, 80 );
legend.ItemArea.Items = pie.GetLegendItems();
legend.ItemArea.BaseLocation = new PointF( 5, 5 );
legend.Title.Visible = false;
legend.Draw( g, new PointF(150, 5) );
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
img.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
}
%>
円グラフ描画クラスTPieChartは、IGraphPainterのクラス階層に属しておらず、
単独のインスタンスとして扱います。
TPieChart pie = new TPieChart();
データを追加した後、TPieChartのDraw()メソッドにグラフィックハンドルを渡して呼び出すと、
イメージバッファに円グラフを描画します。
pie.Draw( g, new PointF(20, 15) );
凡例が必要な場合、TLegendBoxのインスタンスを作成します。
TLegendBox legend = new TLegendBox();
TLegendBox.ItemArea.Itemsプロパティに、TPieChart.GetLegendItems()メソッドの戻り値を設定します。
これで円グラフのデータの凡例が設定されます。
legend.ItemArea.Items = pie.GetLegendItems();
TLegendBoxのDraw()メソッドにグラフィックハンドルを渡して呼び出すと、
イメージバッファに凡例ボックスを描画します。
legend.Draw( g, new PointF(150, 5) );
円グラフを、IGraphPainterのグラフと同じ画像に描画したい場合は、
IGraphPainter.Shapes.Add()メソッドにTPieChartのインスタンスを渡して呼び出します。
後は、IGraphPainterのDraw()メソッドで円グラフも同時に描画されます。
円グラフの描画位置はTPieChart.Locationプロパティで事前に設定します。
IGraphPainter graph = DusGraph.CreateGraph();
pie.Location = new PointF( 20, 10 );
graph.Shapes.Add( pie );
ページ上部へ
下図のグラフを描画するサンプルコードを掲載します(ASP.NET)。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="Dustin.Utils" %>
<%@ Import Namespace="Dustin.Drawing" %>
<%@ Import Namespace="Dustin.Graph" %>
<script runat="server">
</script>
<%
IGraphPainter graph = DusGraph.CreateGraph(300, 200);
graph.PlotArea.Margin.Left = 20;
graph.PlotArea.Margin.Right = 10;
graph.PlotArea.Margin.Top = 20;
graph.PlotArea.Margin.Bottom = 10;
IAxis axis;
axis = graph.YAxis;
axis.Scale.Min = 5;
axis.Scale.Max = 0;
axis.Scale.Base= 0;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
axis = graph.XAxis;
axis.Scale.Min = 0;
axis.Scale.Max = 5;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
axis.Ticks.Side = DusGraph.eTickSide.Top;
axis.Ticks.Labels.Margin = 15;
ISeriesBubbleChart sr = graph.CreateSeries( DusGraph.ePlotType.BubbleChart ).asBubbleChart;
sr.Data.Add( new TBubblePoint( 3, 1, 8, Brushes.Pink ) );
sr.Data.Add( new TBubblePoint(4.5, 2, 18, Brushes.Magenta ) );
sr.Data.Add( new TBubblePoint(1.2, 4, 24, Brushes.SeaGreen) );
graph.Series.Add( sr );
graph.Draw();
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
graph.Image.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
graph.Dispose();
%>
ページ上部へ
下図のグラフを描画するサンプルコードを掲載します(ASP.NET)。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="Dustin.Utils" %>
<%@ Import Namespace="Dustin.Drawing" %>
<%@ Import Namespace="Dustin.Graph" %>
<script runat="server">
</script>
<%
IGraphPainter graph = DusGraph.CreateGraph(300, 200);
graph.PlotArea.Margin.Left = 30;
graph.PlotArea.Margin.Right = 10;
graph.PlotArea.Margin.Top = 10;
graph.PlotArea.Margin.Bottom = 20;
IAxis axis;
axis = graph.YAxis;
axis.Scale.Min = -3;
axis.Scale.Max = 5;
axis.Scale.Base= 0;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
axis = graph.XAxis;
axis.Scale.Min = 0;
axis.Scale.Max = 5;
axis.Grid.Major.Visible = true;
axis.Grid.Major.Interval = 1;
ISeriesCurvePlot sr = graph.CreateSeries( DusGraph.ePlotType.Curve ).asCurve;
sr.Data.Add( new ST_PlotPoint(0, 2) );
sr.Data.Add( new ST_PlotPoint(1, 1.5) );
sr.Data.Add( new ST_PlotPoint(2, -2) );
sr.Data.Add( new ST_PlotPoint(3, 1) );
sr.Data.Add( new ST_PlotPoint(4, 3.6) );
sr.Data.Add( new ST_PlotPoint(5, 3) );
sr.Mark.Visible = true;
sr.Mark.Type = DusGraph.ePlotMarkType.Star;
sr.Mark.Brush = Brushes.LightBlue;
sr.Mark.Width = 10;
sr.Mark.Height = 10;
sr.Mark.Border.Pen = Pens.Blue;
sr.Pen = Pens.Blue;
sr.Tension = 0.7f;
sr.HasWall = true;
sr.WallBrush = Brushes.SkyBlue;
graph.Series.Add( sr );
graph.Draw();
Response.ContentType = "image/png";
MemoryStream o = new MemoryStream();
graph.Image.Save( o, ImageFormat.Png );
o.WriteTo( Response.OutputStream );
graph.Dispose();
%>
ページ上部へ
|
OnDrawBackground、OnDrawForeground
|
IGraphPainter.OnDrawBackgroundイベントは、背景の描画時に発生します。
IGraphPainter.OnDrawForegroundイベントは、前景の描画時に発生します。
例えばグラフの背景に画像を描画する場合は下記のようにします。
graph.OnDrawBackground += delegate( Object sender, TDrawEventArgs e ) {
String fpath = Server.MapPath( "./picture.png" );
using( System.Drawing.Image img = System.Drawing.Image.FromFile(fpath) ) {
e.Graphics.DrawImage( img, new Point(0, 0) );
}
};
ページ上部へ