好的,我已经解决了问题。必须是double [] xvaluesTmp,并且赋值:xvaluesTmp [0] = DateTime.Now.ToOADate()(作为double),现在工作正常
我下载了Microsoft Charts控件(System.Windows.Forms.DataVisualization.dll)。 要显示小时,分钟和秒,我有格式:
chartArea.AxisX.LabelStyle.Format = "{HH}:{mm}:{ss}";
它显示了严谨性。但看起来格式如何显示年月日?我尝试从文档:“D”,“G”,“M”,“d”,“y”。它显示了我的日期,但始终显示1899-30-01或1899年1月30日等。我传递给DataBinding DateTime.Now:
List<DateTime> xvaluesTmp = new List<DateTime>();
xvaluesTmp.Add(DateTime.Now);
xvaluesTmp.Add(new DateTime(2011, 3, 28));
...
this.chart.Series[0].Points.DataBindXY(xvaluesTmp, yvaluesTmp);
有什么问题?
由于
答案 0 :(得分:0)
这个问题有点模糊,但是对于1899日期显示有问题的人,请确保将ChartValueType设置为DateTime。
如果ChartValueType设置为Time,则会显示1899日期。
this.mainChart.ChartTypeSelected.Series[0].XValueType = ChartValueType.DateTime;
然后将标签格式化为您需要的
this.mainChart.ChartTypeSelected.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
答案 1 :(得分:0)
我已经有这个问题已经有一段时间了,终于通过以下方式解决了这个问题:
附加到图表的自定义事件:
_chart.Customize += Remove_1899_Label;
在Remove_1899_Label事件处理程序中:
private void Remove_1899_Label(object sender, EventArgs e)
{
Chart msChart = sender as Chart;
foreach (var chartArea in msChart.ChartAreas)
{
foreach (var label in chartArea.AxisX.CustomLabels)
{
if (!string.IsNullOrEmpty(label.Text) && label.Text == "1899")
{
label.SetFieldValue("customLabel", true);
label.Text = string.Empty;
}
}
}
}
N.B出于某种原因,除非您还设置名为&#39; customLabel&#39;的私有bool成员变量,否则更改标签文本并不起作用。标签为true。扩展方法的实现&#39; SetFieldValue(string,object)&#39;我从这里来了: http://dotnetfollower.com/wordpress/2013/06/c-how-to-set-or-get-value-of-a-private-or-internal-field-through-the-reflection/
但是,为了完整起见,我已将其包括在内。
public static class ReflectionHelper
{
//...
// here are methods described in the post
// http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/
//...
private static FieldInfo GetFieldInfo(Type type, string fieldName)
{
FieldInfo fieldInfo;
do
{
fieldInfo = type.GetField(fieldName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
type = type.BaseType;
}
while (fieldInfo == null && type != null);
return fieldInfo;
}
public static object GetFieldValue(this object obj, string fieldName)
{
if (obj == null)
throw new ArgumentNullException("obj");
Type objType = obj.GetType();
FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
if (fieldInfo == null)
throw new ArgumentOutOfRangeException("fieldName",
string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
return fieldInfo.GetValue(obj);
}
public static void SetFieldValue(this object obj, string fieldName, object val)
{
if (obj == null)
throw new ArgumentNullException("obj");
Type objType = obj.GetType();
FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
if (fieldInfo == null)
throw new ArgumentOutOfRangeException("fieldName",
string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
fieldInfo.SetValue(obj, val);
}
}