【问题标题】:devexpress xtracharts showing date onlydevexpress xtracharts 仅显示日期
【发布时间】:2014-10-14 21:48:50
【问题描述】:

我创建了一个 devexpress 图表 (v13.2),它在 x 轴上具有日期时间,在 Y 轴上具有值。

目前在 x 轴上显示日期如下:

但我也需要默认显示日期和时间,如下所示:

即使 x 轴被编码为从数据库中获取日期时间,但它仍然只显示日期而不是时间。 为什么?

【问题讨论】:

  • 您能否提供 x- 参数和 y- 值数据来了解确切的问题。你如何修改 x 轴值(意味着获取)
  • @NiranjanKala 请看我的编辑
  • @NiranjanKala 目前我主要关心的是如何在 x 轴中显示日期时间,因为目前 x 轴显示日期但没有时间,我使用 XYDiagram d =(XYDiagram)chartControl1.Diagram 作为 XYDiagram; d.AxisX.Label.DateTimeOptions.Format = DateTimeFormat.General;根据 DmitryG 在答案部分的回复,但我收到空引用错误
  • 空引用,哪一段代码。
  • 您没有设置哪个 Y 轴将指示哪个系列的值。您是否错过了或在其他地方设置了一些??

标签: c# datetime charts devexpress


【解决方案1】:

这是在刻度中显示日期和时间的两步过程:

  1. Axis.DateTimeOptions.Format 属性设置为DateTimeFormat.General
  2. 然后是自定义设置的 Axis.DateTimeScaleOptions 属性。

    (XYDiagram)chartControl1.Diagram).AxisX.DateTimeOptions.Format = DateTimeFormat.General;
    (XYDiagram)chartControl1.Diagram).AxisX.DateTimeScaleOptions.GridAlignment = 
                                    DevExpress.XtraCharts.DateTimeGridAlignment.Minute;
    (XYDiagram)chartControl1.Diagram).AxisX.DateTimeScaleOptions.MeasureUnit = 
                                      DevExpress.XtraCharts.DateTimeMeasureUnit.Minute;
    

检查以下图表的代码和配置,这使我能够获得您想要完成的所需结果:

Form.cs:

public Form1()
{
    InitializeComponent();
    // This line of code is generated by Data Source Configuration Wizard
    chartDataTableAdapter1.Fill(dbDataSet1.ChartData);
}

Designer.cs:

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    DevExpress.XtraCharts.XYDiagram xyDiagram1 = new DevExpress.XtraCharts.XYDiagram();
    DevExpress.XtraCharts.PointSeriesLabel pointSeriesLabel1 = new DevExpress.XtraCharts.PointSeriesLabel();
    DevExpress.XtraCharts.PointOptions pointOptions1 = new DevExpress.XtraCharts.PointOptions();
    DevExpress.XtraCharts.LineSeriesView lineSeriesView1 = new DevExpress.XtraCharts.LineSeriesView();
    this.chartControl1 = new DevExpress.XtraCharts.ChartControl();
    this.chartDataTableAdapter1 = new ChartTest.DBDataSetTableAdapters.ChartDataTableAdapter();
    this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
    this.dbDataSet1 = new ChartTest.DBDataSet();
    ((System.ComponentModel.ISupportInitialize)(this.chartControl1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(xyDiagram1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(pointSeriesLabel1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(lineSeriesView1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.dbDataSet1)).BeginInit();
    this.SuspendLayout();
    // 
    // chartControl1
    // 
    this.chartControl1.DataAdapter = this.chartDataTableAdapter1;
    this.chartControl1.DataSource = this.bindingSource1;
    xyDiagram1.AxisX.DateTimeScaleOptions.GridAlignment = DevExpress.XtraCharts.DateTimeGridAlignment.Minute;
    xyDiagram1.AxisX.DateTimeScaleOptions.MeasureUnit = DevExpress.XtraCharts.DateTimeMeasureUnit.Minute;
    xyDiagram1.AxisX.Label.DateTimeOptions.Format = DevExpress.XtraCharts.DateTimeFormat.General;
    xyDiagram1.AxisX.Range.AlwaysShowZeroLevel = true;
    xyDiagram1.AxisX.Range.ScrollingRange.SideMarginsEnabled = true;
    xyDiagram1.AxisX.Range.SideMarginsEnabled = true;
    xyDiagram1.AxisX.VisibleInPanesSerializable = "-1";
    xyDiagram1.AxisY.Range.AlwaysShowZeroLevel = true;
    xyDiagram1.AxisY.Range.ScrollingRange.SideMarginsEnabled = true;
    xyDiagram1.AxisY.Range.SideMarginsEnabled = true;
    xyDiagram1.AxisY.VisibleInPanesSerializable = "-1";
    this.chartControl1.Diagram = xyDiagram1;
    this.chartControl1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.chartControl1.Legend.Visible = false;
    this.chartControl1.Location = new System.Drawing.Point(0, 0);
    this.chartControl1.Name = "chartControl1";
    this.chartControl1.SeriesDataMember = "VariableName";
    this.chartControl1.SeriesSerializable = new DevExpress.XtraCharts.Series[0];
    this.chartControl1.SeriesTemplate.ArgumentDataMember = "LastTime";
    this.chartControl1.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.DateTime;
    pointSeriesLabel1.LineVisible = true;
    pointOptions1.ArgumentDateTimeOptions.Format = DevExpress.XtraCharts.DateTimeFormat.General;
    pointSeriesLabel1.PointOptions = pointOptions1;
    this.chartControl1.SeriesTemplate.Label = pointSeriesLabel1;
    this.chartControl1.SeriesTemplate.ValueDataMembersSerializable = "LastValue";
    this.chartControl1.SeriesTemplate.View = lineSeriesView1;
    this.chartControl1.Size = new System.Drawing.Size(284, 262);
    this.chartControl1.TabIndex = 0;
    // 
    // chartDataTableAdapter1
    // 
    this.chartDataTableAdapter1.ClearBeforeFill = true;
    // 
    // bindingSource1
    // 
    this.bindingSource1.DataMember = "ChartData";
    this.bindingSource1.DataSource = this.dbDataSet1;
    this.bindingSource1.Sort = "";
    // 
    // dbDataSet1
    // 
    this.dbDataSet1.DataSetName = "DBDataSet";
    this.dbDataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
    this.Controls.Add(this.chartControl1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(xyDiagram1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(pointSeriesLabel1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(lineSeriesView1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.chartControl1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.dbDataSet1)).EndInit();
    this.ResumeLayout(false);

}

#endregion

private DevExpress.XtraCharts.ChartControl chartControl1;
private DBDataSetTableAdapters.ChartDataTableAdapter chartDataTableAdapter1;
private System.Windows.Forms.BindingSource bindingSource1;
private DBDataSet dbDataSet1;

图表数据:

结果:

【讨论】:

    【解决方案2】:

    尝试Axis.DateTimeOptions 属性并设置适当的DateTimeFormat 值:

    ((XYDiagram)chartControl1.Diagram).AxisX.DateTimeOptions.Format = DateTimeFormat.General;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2014-05-07
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多