DevExpress之ChartControl实现柱状图演示实例
本文实例讲述了devexpress中chartcontrol实现柱状图演示的方法。分享给大家供大家参考。具体实现方法如下:
关键代码如下:
using system.data;
using system.windows.forms;
using csharputilhelpv2;
using devexpress.xtracharts;
namespace devexpresschart
{
public partial class winproduct : form
{
public winproduct()
{
initializecomponent();
}
static readonly string accessfullpath = string.format(@"{0}\northwind.mdb", appdomain.currentdomain.basedirectory);
static datatable productlist = null;
accessutilsv2 accesshelper = null;
private void winchat_load(object sender, eventargs e)
{
accesshelper = new accessutilsv2(accessfullpath);
productlist = accesshelper.executedatatable("select * from products", null);
builderdevchart();
}
private void builderdevchart()
{
if (productlist != null)
{
series _barseries = new series("测试", viewtype.bar);
_barseries.argumentdatamember = "productname";//x轴
_barseries.valuedatamembers[0] = "unitprice";//y轴
_barseries.datasource = productlist;
_barseries.adddatafilter("categoryid", 4, datafiltercondition.equal);
_barseries.setcoloreach(true);
chartcontrol1.series.add(_barseries);
_barseries.legendpointoptions.pointview = pointview.argumentandvalues;
//chartcontrol1.seriesdatamember = "unitprice";
//chartcontrol1.seriestemplate.valuedatamembers[0] = "unitprice";
//chartcontrol1.seriestemplate.argumentdatamember = "productname";
//chartcontrol1.seriestemplate.argumentscaletype = scaletype.qualitative;
//chartcontrol1.seriestemplate.adddatafilter("categoryid", 4, datafiltercondition.equal);
//chartcontrol1.seriestemplate.legendpointoptions.pointview = pointview.argumentandvalues;
chartcontrol1.setxlableangle(-35);
chartcontrol1.setcrosshair(true);
chartcontrol1.legend.direction = legenddirection.lefttoright;
chartcontrol1.legend.alignmenthorizontal = legendalignmenthorizontal.center;
chartcontrol1.legend.alignmentvertical = legendalignmentvertical.bottomoutside;
chartcontrol1.datasource = productlist;
chartcontrol1.addtitle("product list");
//--------------------------
}
}
}
}
using devexpress.xtracharts;
namespace devexpresschart
{
public static class chartutils
{
/// <summary>
/// 增加数据筛选
/// </summary>
/// <param name="seriesbase">series</param>
/// <param name="columnname">列名称</param>
/// <param name="value">列名称对应的筛选数值</param>
/// <param name="datafiltercondition">datafiltercondition枚举</param>
public static void adddatafilter(this seriesbase series, string columnname, object value, datafiltercondition datafiltercondition)
{
series.datafilters.add(new datafilter(columnname, value.gettype().fullname, datafiltercondition, value));
}
/// <summary>
/// 设置x轴lable角度
/// </summary>
/// <param name="chart">chartcontrol</param>
/// <param name="angle">角度</param>
public static void setxlableangle(this chartcontrol chart, int angle)
{
xydiagram _xydiagram = (xydiagram)chart.diagram;
if (_xydiagram != null)
_xydiagram.axisx.label.angle = angle;
}
/// <summary>
/// 设置y轴lable角度
/// </summary>
/// <param name="chart">chartcontrol</param>
/// <param name="angle">角度</param>
public static void setylableangle(this chartcontrol chart, int angle)
{
xydiagram _xydiagram = (xydiagram)chart.diagram;
_xydiagram.axisy.label.angle = angle;
}
/// <summary>
/// 设置coloreach
/// </summary>
/// <param name="chart">chartcontrol</param>
/// <param name="coloreach">是否设置成coloreach</param>
public static void setcoloreach(this series series, bool coloreach)
{
seriesviewcoloreachsupportbase coloreachview = (seriesviewcoloreachsupportbase)series.view;
if (coloreachview != null)
{
coloreachview.coloreach = coloreach;
}
}
/// <summary>
/// 设置是否显示十字标线
/// </summary>
/// <param name="chart">chartcontrol</param>
/// <param name="crosshair">是否显示十字标线</param>
public static void setcrosshair(this chartcontrol chart, bool crosshair)
{
chart.crosshairenabled = crosshair ? defaultboolean.true : defaultboolean.false;
chart.crosshairoptions.showargumentlabels = crosshair;
chart.crosshairoptions.showargumentline = crosshair;
chart.crosshairoptions.showvaluelabels = crosshair;
chart.crosshairoptions.showvalueline = crosshair;
}
/// <summary>
/// 新增chartcontrol的title文字
/// </summary>
/// <param name="chart">chartcontrol</param>
/// <param name="title">title文字</param>
public static void addtitle(this chartcontrol chart, string title)
{
charttitle _title = new charttitle();
_title.text = title;
chart.titles.add(_title);
}
/// <summary>
/// 饼状series设置成百分比显示
/// </summary>
/// <param name="series">series</param>
public static void setpiepercentage(this series series)
{
if (series.view is pieseriesview)
{
((piepointoptions)series.pointoptions).percentoptions.valueaspercent = true;
((piepointoptions)series.pointoptions).valuenumericoptions.format = numericformat.percent;
((piepointoptions)series.pointoptions).valuenumericoptions.precision = 0;
}
}
}
}
运行效果如下图所示:
希望本文所述对大家的c#程序设计有所帮助。