DevExpress之ChartControl完成時光軸實例。本站提示廣大學習愛好者:(DevExpress之ChartControl完成時光軸實例)文章只能為提供參考,不一定能成為您想要的結果。以下是DevExpress之ChartControl完成時光軸實例正文
本文實例講述了DevExpress采取ChartControl完成時光軸的辦法,分享給年夜家供年夜家參考。詳細完成辦法以下:
症結代碼以下:
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
namespace DevExpressChart
{
public partial class winDateTime : Form
{
public winDateTime()
{
InitializeComponent();
}
private void winDateTime_Load(object sender, EventArgs e)
{
BuilderDevChart();
}
private DataTable CreateChartData()
{
DataTable table = new DataTable("Table1");
table.Columns.Add("Date", typeof(DateTime));
table.Columns.Add("Value", typeof(Int32));
table.Rows.Add(new object[] { new DateTime(2014, 5, 21), 10 });
table.Rows.Add(new object[] { new DateTime(2014, 6, 22), 20 });
table.Rows.Add(new object[] { new DateTime(2014, 7, 23), 40 });
table.Rows.Add(new object[] { new DateTime(2014, 8, 24), 20 });
table.Rows.Add(new object[] { new DateTime(2014, 9, 25), 30 });
return table;
}
private void BuilderDevChart()
{
Series _lineSeries = new Series("利潤", ViewType.Line);
_lineSeries.ArgumentScaleType = ScaleType.DateTime;
_lineSeries.ArgumentDataMember = "Date";
_lineSeries.ValueDataMembers[0] = "Value";
_lineSeries.DataSource = CreateChartData();
chartControl1.Series.Add(_lineSeries);
chartControl1.SetTimeAxisX(DateTimeMeasurementUnit.Month, DateTimeMeasurementUnit.Month, "yyyy-MM");
//---------------------------------------------------------------
//_lineSeries.ValueScaleType = ScaleType.DateTime;
//_lineSeries.ArgumentDataMember = "Value";
//_lineSeries.ValueDataMembers[0] = "Date";
//_lineSeries.DataSource = CreateChartData();
//chartControl1.Series.Add(_lineSeries);
//XYDiagram _diagram = (XYDiagram)chartControl1.Diagram;
//_diagram.AxisY.DateTimeMeasureUnit = DateTimeMeasurementUnit.Month;
//_diagram.AxisY.DateTimeGridAlignment = DateTimeMeasurementUnit.Month;
//_diagram.AxisY.DateTimeOptions.Format = DateTimeFormat.Custom;
//_diagram.AxisY.DateTimeOptions.FormatString = "MMMM";
}
}
}
using DevExpress.Utils;
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;
}
}
/// <summary>
/// 將X軸格局化成時光軸
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="dateTimeMeasureUnit">X軸刻度單元</param>
/// <param name="dateTimeGridAlignment">X軸刻度間距的單元</param>
public static void SetTimeAxisX(this ChartControl chart, DateTimeMeasurementUnit dateTimeMeasureUnit, DateTimeMeasurementUnit dateTimeGridAlignment)
{
XYDiagram _diagram = (XYDiagram)chart.Diagram;
if (_diagram != null)
{
_diagram.AxisX.DateTimeMeasureUnit = DateTimeMeasurementUnit.Month;//X軸刻度單元
_diagram.AxisX.DateTimeGridAlignment = DateTimeMeasurementUnit.Month;//X軸刻度間距
}
}
/// <summary>
/// 將X軸格局化成時光軸
/// </summary>
/// <param name="chart">ChartControl</param>
/// <param name="dateTimeMeasureUnit">X軸刻度單元</param>
/// <param name="dateTimeGridAlignment">X軸刻度間距的單元</param>
/// <param name="formatString">時光格局;eg:yyyy-MM</param>
public static void SetTimeAxisX(this ChartControl chart, DateTimeMeasurementUnit dateTimeMeasureUnit, DateTimeMeasurementUnit dateTimeGridAlignment, string formatString)
{
XYDiagram _diagram = (XYDiagram)chart.Diagram;
if (_diagram != null)
{
_diagram.AxisX.DateTimeMeasureUnit = DateTimeMeasurementUnit.Month;//X軸刻度單元
_diagram.AxisX.DateTimeGridAlignment = DateTimeMeasurementUnit.Month;//X軸刻度間距
_diagram.AxisX.DateTimeOptions.Format = DateTimeFormat.Custom;
_diagram.AxisX.DateTimeOptions.FormatString = formatString;
}
}
}
}
運轉後果以下圖所示:
願望本文所述對年夜家的C#法式設計有所贊助