ASP.NET图表控件从X轴标签上的int值设置月份名称 [英] ASP.NET Chart control set month names from int value on X Axis labels

查看:217
本文介绍了ASP.NET图表控件从X轴标签上的int值设置月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用对应的月份名称替换图表X轴上的数字(1,2,3,...)。

这是我的实际图表:



在填充系列 DataView 中,我有MONTH包含值为1到12的列。
我也必须手动设置最小值最大值值到0和13,否则我失去了第一列和最后一列,因为第一个X轴列的开始和Y轴之间以及Chart区域的最后一个X轴列结束之间没有空格。

 < ChartAreas> 
< asp:ChartArea Name =ChartArea1>
TitleFont =Segoe UI,10pt,style = BoldIsLabelAutoFit =FalseLineColor =Gray
Minimum = 0最大=13>
< MajorGrid LineColor =LightGray/>
< LabelStyle Font =Segoe UI,9pt/>
< / AxisX>
< / asp:ChartArea>
< / ChartAreas>所以,如果我用月份名称替换int值,我还需要删除最小和最大参数,但我想保持现在的空间。



我尝试了一些在这里和其他地方建议的解决方案,但没有结果。
最简单,最有效,最直接的就是这样:

pre $ Chart $ Chart $ Chart .Format =MMM;

但是不起作用。相反,我得到MMM字符串来代替整数值。

是否可以拦截 DataBind 事件就像在一个GridView上,用月份名称替换整数值?解析方案

我认为你必须使用自定义标签。
我使用了这个

使用系统; 
using System.Collections.Generic;
使用System.Globalization;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;

namespace WebTest_1
{
public partial class _Default:Page
{
protected void Page_Load(object sender,EventArgs e)
{
Chart1.Series [0] .Points.AddXY(1,83);
Chart1.Series [0] .Points.AddXY(2,63);
Chart1.Series [0] .Points.AddXY(3,53);
Chart1.Series [0] .Points.AddXY(4,77);
Chart1.Series [0] .Points.AddXY(5,46);
Chart1.Series [0] .Points.AddXY(6,99);
Chart1.Series [0] .Points.AddXY(7,72);
Chart1.Series [0] .Points.AddXY(8,39);
Chart1.Series [0] .Points.AddXY(9,42);
Chart1.Series [0] .Points.AddXY(10,71);
Chart1.Series [0] .Points.AddXY(11,58);
Chart1.Series [0] .Points.AddXY(12,63);

Chart1.Series [1] .Points.AddXY(1,46);
Chart1.Series [1] .Points.AddXY(2,72);
Chart1.Series [1] .Points.AddXY(3,53);
Chart1.Series [1] .Points.AddXY(4,39);
Chart1.Series [1] .Points.AddXY(5,63);
Chart1.Series [1] .Points.AddXY(6,71);
Chart1.Series [1] .Points.AddXY(7,75);
Chart1.Series [1] .Points.AddXY(8,99);
Chart1.Series [1] .Points.AddXY(9,83);
Chart1.Series [1] .Points.AddXY(10,63);
Chart1.Series [1] .Points.AddXY(11,58);
Chart1.Series [1] .Points.AddXY(12,42);


protected void Chart1_Customize(object sender,EventArgs e)
{
foreach(Chart1.ChartAreas [0] .AxisX.CustomLabels中的变量lbl)
{
int monthNumber = int.Parse(lbl.Text); (月份数> = 1&月份数< = 12)

lbl.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNumber);
else
lbl.Text =;






输出:





您需要指定Customize事件。


I want to replace numbers (1, 2, 3, ...) on the X Axis of the chart with the corresponding month name.

This is my actual chart:

On the DataView that populates the Series I have the "MONTH" column that contains values from 1 to 12. I also had to manually set the Minimum and Maximum value to 0 and 13, otherwise I lose the first and last column because of a lack of space between the beginning of the first X Axis column and the Y Axis and between the last X Axis column end of Chart area.

<ChartAreas>
    <asp:ChartArea Name="ChartArea1">
        <AxisX IsStartedFromZero="True" Title="Mese"
            TitleFont="Segoe UI, 10pt, style=Bold" IsLabelAutoFit="False" LineColor="Gray"
            Minimum="0" Maximum="13">
            <MajorGrid LineColor="LightGray" />
            <LabelStyle Font="Segoe UI, 9pt" />
        </AxisX>
    </asp:ChartArea>
</ChartAreas>

So, if I replace the int values with month names I also need to remove the Min and Max parameters, but I want to keep the space like it is now.

I tried with some solutions suggested here and elsewhere but with no results. The easiest, most effective and immediate seemed to be this:

Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MMM";

but it doesn't work. Instead I get "MMM" string in place of the integer values.

Is it possible to intercept the DataBind event like on a GridView, to replace the integer values with month names?

解决方案

I think you have to use custom labels. I used this and this.

The code behind:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTest_1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Chart1.Series[0].Points.AddXY(1, 83);
            Chart1.Series[0].Points.AddXY(2, 63);
            Chart1.Series[0].Points.AddXY(3, 53);
            Chart1.Series[0].Points.AddXY(4, 77);
            Chart1.Series[0].Points.AddXY(5, 46);
            Chart1.Series[0].Points.AddXY(6, 99);
            Chart1.Series[0].Points.AddXY(7, 72);
            Chart1.Series[0].Points.AddXY(8, 39);
            Chart1.Series[0].Points.AddXY(9, 42);
            Chart1.Series[0].Points.AddXY(10, 71);
            Chart1.Series[0].Points.AddXY(11, 58);
            Chart1.Series[0].Points.AddXY(12, 63);

            Chart1.Series[1].Points.AddXY(1 , 46);
            Chart1.Series[1].Points.AddXY(2 , 72);
            Chart1.Series[1].Points.AddXY(3 , 53);
            Chart1.Series[1].Points.AddXY(4 , 39);
            Chart1.Series[1].Points.AddXY(5 , 63);
            Chart1.Series[1].Points.AddXY(6 , 71);
            Chart1.Series[1].Points.AddXY(7 , 75);
            Chart1.Series[1].Points.AddXY(8 , 99);
            Chart1.Series[1].Points.AddXY(9 , 83);
            Chart1.Series[1].Points.AddXY(10, 63);
            Chart1.Series[1].Points.AddXY(11, 58);
            Chart1.Series[1].Points.AddXY(12, 42);
        }

        protected void Chart1_Customize(object sender, EventArgs e)
        {
            foreach (var lbl in Chart1.ChartAreas[0].AxisX.CustomLabels)
            {
                int monthNumber = int.Parse(lbl.Text);
                if (monthNumber >= 1 && monthNumber <= 12)
                    lbl.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNumber);
                else
                    lbl.Text = "";
            }
        }
    }
}

Output:

You need to assign the Customize event.

这篇关于ASP.NET图表控件从X轴标签上的int值设置月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆