OpenFlashChart使用
最近在做的项目需要经常使用图表,在使用了MSchart之后发现例子少,而且极其难控制.在网上搜了一下,找到OpenFlashChart这个开源项目,发现效果还不错,而且例子满全的,效果也不错.官方下载地址:http://teethgrinder.co.uk/open-flash-chart/.
该控件的数据传输方式是采用了JSON,所以有个C#写得项目是用来翻译把调用代码生成JSON代码.
下面贴下使用方法备忘:
1.编译openflashchart工程生成DLL引用到项目中.
2.在VS工具栏上加上该控件.
各种图形的生成代码:
1.饼图:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
chart.Title = new Title("Pie Chart");
OpenFlashChart.Pie pie = new OpenFlashChart.Pie();
Random random = new Random();
List<PieValue> values = new List<PieValue>();
List<string> labels = new List<string>();
for (int i = 0; i < 12; i++)
{
values.Add(new PieValue(random.NextDouble(),"Pie"+i));
labels.Add(i.ToString());
}
//values.Add(0.2);
PieValue pieValue = new PieValue(10);
pieValue.Click = "http://xiao-yifang.blogspot.com";
values.Add(pieValue);
pie.Values = values;
pie.FontSize = 20;
pie.Alpha = .5;
AnimationSeries animationSeries = new AnimationSeries();
animationSeries.Add(new Animation("bounce",5));
pie.Animate = animationSeries;
//pie.GradientFillMode = false;
//pie.FillAlpha = 10;
//pie.Colour = "#fff";
pie.Colours = new string[]{"#04f","#1ff","#6ef","#f30"};
pie.Tooltip="#label#,#val# of #total##percent# of 100%";
chart.AddElement(pie);
chart.Bgcolor = "#202020";
柱状图:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
chart.Title = new Title("Bar Chart");
Bar bar = new OpenFlashChart.Bar();
Random random = new Random();
bar.Colour = "#344565";
bar.FillAlpha = 0.4;
bar.Text = "Test";
bar.FontSize = 10;
List<object> values = new List<object>();
for (int i = 0; i < 12; i++)
values.Add(random.Next(i, i * 2));
BarValue barValue = new BarValue(12);
barValue.OnClick = "http://xiao-yifang.blogspot.com";
values.Add(barValue);
bar.Values = values;
chart.AddElement(bar);
XAxis xaxis = new XAxis();
xaxis.Labels.SetLabels(new string[] { "text", "#ef0", "10", "vertical" });
//xaxis.Steps = 1;
//xaxis.Offset = true;
////xaxis.SetRange(-2, 15);
chart.X_Axis = xaxis;
//YAxis yaxis = new YAxis();
//yaxis.Steps = 4;
//yaxis.SetRange(0, 20);
//chart.Y_Axis = yaxis;
chart.Y_Axis.SetRange(0,24,3);
bar.Tooltip = "提示:label:#x_label#<br>#top#<br>#bottom#<br>#val#";
线状图:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
List<double> data1 = new List<double>();
List<double> data2 = new List<double>();
Random rand = new Random(DateTime.Now.Millisecond);
for(double i=0;i<12;i++)
{
data1.Add(rand.Next(30));
data2.Add(rand.Next(50));
}
OpenFlashChart.Line line1 = new Line();
line1.Values = data1;
line1.HaloSize = 0;
line1.Width = 2;
line1.DotSize = 5;
line1.DotStyleType.Tip = "#x_label#<br>#val#";
//line1.DotStyleType.Type = DotType.ANCHOR;
//line1.DotStyleType.Type = DotType.BOW;
line1.DotStyleType.Colour = "#467533";
line1.Tooltip = "#x_label#<br>提示:#val#";
OpenFlashChart.Line line2 = new Line();
line2.Values = data2;
line2.HaloSize = 1;
line2.Width = 3;
line2.DotSize = 4;
line2.DotStyleType.Tip = "#x_label#<br>#val#";
line1.DotStyleType.Type = DotType.ANCHOR;
//line1.DotStyleType.Type = DotType.BOW;
line2.DotStyleType.Colour = "#fe4567";
line2.Tooltip = "提示:#val#";
line2.AttachToRightAxis(true);
chart.AddElement(line1);
chart.AddElement(line2);
chart.Y_Legend=new Legend("中文test");
chart.Title = new Title("line演示");
chart.Y_Axis.SetRange(0,35,5);
chart.X_Axis.Labels.Color = "#e43456";
chart.X_Axis.Labels.VisibleSteps = 3;
chart.Y_Axis.Labels.FormatString = "$#val#";
chart.X_Axis.SetLabels(new string[]{"test1","test2"});
//chart.Y_Axis.SetLabels(new string[] { "test1", "test2", "test1", "test2", "test1", "test2" });
chart.X_Axis.Steps = 4;
chart.Y_Axis.Steps = 3;
chart.Y_Axis.Colour = "#ef6745";
chart.Y_Axis.Labels.Color = "#ef6745";
chart.Y_Axis.Offset = true;
chart.Y_RightLegend = new Legend("test y legend right");
chart.Y_Axis_Right = new YAxis();
chart.Y_Axis_Right.Steps = 8;
chart.Y_Axis_Right.TickLength = 4;
chart.Y_Axis.TickLength = 4;
chart.Y_Axis_Right.SetRange(0,60);
chart.Tooltip = new ToolTip("全局提示:#val#");
chart.Tooltip.Shadow = true;
chart.Tooltip.Colour = "#e43456";
chart.Tooltip.MouseStyle = ToolTipStyle.CLOSEST;