ASP.NET Chart Series Points AxisLabel 隔列显示 如何解决

VAllens 2012-12-20 10:32:42
想要解决的问题是:
如何全部显示出来?
问题:
在Y轴,每一条柱子下边的字符串都应该显示出来。
共13条柱子,也就是说共有13个字符串,然而现在只显示6个字符串。
原先13个字符串,每个字符串都是很长的,我以为是因为字符串太长导致其它字符串被挤下去,然后我就改成0,1,2,3,4,5,6,7...这些短的字符串代替测试,结果还是一样。
而我发现它是有规律的隔列显示,我以为是某个属性造成的,可是我找不到是哪个。
如图1:

前置代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Result.aspx.cs" Inherits="Result" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:Chart ID="Chart1" runat="server" Height="800px" Width="1920px" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"
BorderDashStyle="Solid" BackSecondaryColor="White" BackGradientStyle="VerticalCenter"
BorderWidth="2px" BackColor="211, 223, 240" BorderColor="#1A3B69">
<Legends>
<asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" TitleAlignment="Center"
Font="Trebuchet MS, 12.5pt, style=Bold">
</asp:Legend>
</Legends>
<Titles>
<asp:Title Text="统计结果" TextStyle="Shadow" Font="Trebuchet MS, 16pt, style=Bold" />
</Titles>
<BorderSkin SkinStyle="Emboss"></BorderSkin>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
BackGradientStyle="TopBottom">
<Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False"
WallWidth="0" IsClustered="False"></Area3DStyle>
<AxisY LineColor="64, 64, 64, 64">
<LabelStyle Font="Trebuchet MS, 12.5pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisY>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</center>
</div>
</form>
</body>
</html>

后置代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Web.UI.DataVisualization.Charting;

public partial class Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Loading();
}
}

/// <summary>
/// 加载图表
/// </summary>
private void Loading()
{
//取消报表网格线
//for (int x = 0; x < Chart1.ChartAreas.Count; x++)
//{
// for (int y = 0; y < Chart1.ChartAreas[x].Axes.Length; y++)
// {
// Chart1.ChartAreas[x].Axes[y].MajorGrid.Enabled = false;
// }
//}

//初始化颜色键值表
Dictionary<string, System.Drawing.Color> colorHash = new Dictionary<string, System.Drawing.Color>();
colorHash.Add("宝安区", System.Drawing.Color.FromArgb(245,168,154));
colorHash.Add("福田区", System.Drawing.Color.FromArgb(241,175,0));
colorHash.Add("罗湖区", System.Drawing.Color.FromArgb(91,189,43));
colorHash.Add("龙岗区", System.Drawing.Color.FromArgb(0,178,191));
colorHash.Add("龙华新区", System.Drawing.Color.FromArgb(99,91,162));
colorHash.Add("南山区", System.Drawing.Color.FromArgb(162,0,124));
colorHash.Add("盐田区", System.Drawing.Color.FromArgb(85, 85, 85));

//添加地区颜色对照表
foreach (string item in colorHash.Keys)
{
Series ssss = new Series(item);
ssss.Color = colorHash[item];
ssss.BorderWidth = 7;
ssss.ShadowOffset = 2;
Chart1.Series.Add(ssss);
break;
}

//设置图表类型
Chart1.Series["宝安区"].Font = new System.Drawing.Font("Trebuchet MS", 24);
//Chart1.Series["宝安区"].ChartType = SeriesChartType.Column;
//Chart1.Series["宝安区"].PostBackValue = "#AXISLABEL" + ";#INDEX";

//查询投票结果
XmlNodeList xnList = this.GetXmlNodeList(@"/datas/Results/result");
List<Results> rList = this.FindData(xnList);
var query = from r in rList
group r by r.OprateId into g
select new
{
g.Key,
Count = g.Count()
};

//查询所有地区
XmlNodeList dataList = this.GetXmlNodeList(@"/datas/data");
//初始化地区键值表
Dictionary<int, XmlNode> dataHash = new Dictionary<int, XmlNode>();
//封装地区键值表
foreach (XmlNode item in dataList)
{
dataHash.Add(Convert.ToInt32(item.Attributes["id"].Value),item);
}
//移除未被选中的地区
foreach (var q in query)
{
dataHash.Remove(q.Key);
}

//初始化所有投票结果集合
List<PollResults> prList = new List<PollResults>();
//封装未投票结果集合
foreach (XmlNode item in dataHash.Values)
{
PollResults pr= new PollResults();
pr.Count = 0;
pr.Name = item.Attributes["name"].Value;
pr.Type = item.Attributes["type"].Value;
pr.OprateId = Convert.ToInt32(item.Attributes["id"].Value);
prList.Add(pr);
}
//封装已投票结果集合
foreach (var q in query)
{
PollResults pr = new PollResults();
pr.Count = q.Count;
XmlNode data = this.GetXmlNode(string.Format(@"/datas/data[@id={0}]", q.Key));
pr.Name = data.Attributes["name"].Value;
pr.Type = data.Attributes["type"].Value;
pr.OprateId = q.Key;
prList.Add(pr);
}

int i = 0;
foreach (PollResults item in prList)
{
//Y轴数据
Chart1.Series["宝安区"].Points.AddY(item.Count);
//X轴显示的名称
Chart1.Series["宝安区"].Points[i].AxisLabel = item.Name;
//Chart1.Series["宝安区"].Points[i].Font = new System.Drawing.Font("Trebuchet MS", 20);
//顶部显示的数字
Chart1.Series["宝安区"].Points[i].Label = item.Count.ToString();
//鼠标放上去的提示内容
Chart1.Series["宝安区"].Points[i].ToolTip = item.Type;
//设置柱子头标签文本颜色
Chart1.Series["宝安区"].Points[i].LabelForeColor = colorHash[item.Type];
//柱子颜色
Chart1.Series["宝安区"].Points[i].Color = colorHash[item.Type];
if (item.Count > 0)
{
Chart1.Series["宝安区"].Points[i].MarkerStyle = MarkerStyle.Triangle;//显示三角形
Chart1.Series["宝安区"].Points[i].MarkerColor = colorHash[item.Type]; //三角形颜色
Chart1.Series["宝安区"].Points[i].MarkerSize = 80;//三角形大小
}
i++;
}

//按照升序的方式排列
//Chart1.Series["宝安区"].Sort(PointSortOrder.Ascending);
}

/// <summary>
/// 封装Results数据集
/// </summary>
/// <param name="xnList"></param>
/// <returns></returns>
private List<Results> FindData(XmlNodeList xnList)
{
List<Results> rList = new List<Results>();
foreach (XmlNode item in xnList)
{
Results r = new Results();
r.Id = Convert.ToInt32(item.Attributes["id"].Value);
r.IpAddress = item.Attributes["ip"].Value;
r.OprateId = Convert.ToInt32(item.Attributes["oprateId"].Value);
rList.Add(r);
}
return rList;
}

/// <summary>
/// 获取节点集
/// </summary>
/// <param name="XPath"></param>
/// <returns></returns>
private XmlNodeList GetXmlNodeList(string XPath)
{
XmlDocument xd = new XmlDocument();
string path = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["xmlPath"]);
xd.Load(path);
return xd.SelectNodes(XPath);
}

/// <summary>
/// 获取节点
/// </summary>
/// <param name="XPath"></param>
/// <returns></returns>
private XmlNode GetXmlNode(string XPath)
{
XmlDocument xd = new XmlDocument();
string path = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["xmlPath"]);
xd.Load(path);
return xd.SelectSingleNode(XPath);
}
}

发文章发不了那么多字,我在回复里再发web.config和xml数据文件吧。
在此谢过各位了。
...全文
643 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
terran_01 2015-03-18
  • 打赏
  • 举报
回复
Chart1.ChartAreas["宝安区"].AxisX.Interval = 1;
VAllens 2012-12-25
  • 打赏
  • 举报
回复
引用 7 楼 Q1092926267 的回复:
引用 6 楼 caizz520 的回复:引用 4 楼 Q1092926267 的回复:chart控件有所了解 兄弟 你贴这么多代码谁看啊 为了四十分 看个半天 跟你说 你这个直接设置X轴最大最小值 和间隔多少就可以了 Chart1.ChartAreas[0].AxisX.Interval = 1; 自己多试试 我刚学Chart……
谢谢
six-years 2012-12-20
  • 打赏
  • 举报
回复
chart控件有所了解 兄弟 你贴这么多代码谁看啊 为了四十分 看个半天 跟你说 你这个直接设置X轴最大最小值 和间隔多少就可以了 Chart1.ChartAreas[0].AxisX.Interval = 1; 自己多试试
VAllens 2012-12-20
  • 打赏
  • 举报
回复
App_Code文件夹内,创建Class1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

[Serializable]
public class Class1
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Height { get; set; }
    public string Url { get; set; }
    public string Desc { get; set; }
}

[Serializable]
public class Results
{
    public int Id { get; set; }
    public int OprateId { get; set; }
    public string IpAddress { get; set; }
}

[Serializable]
public struct PollResults
{
    public int OprateId { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public int Count { get; set; }
}
VAllens 2012-12-20
  • 打赏
  • 举报
回复
附上解决方案:http://pan.baidu.com/share/link?shareid=164464&uk=3104341340 如果不想这么麻烦的,可以下载这个解决方案,以便帮助我解决问题,该RAR压缩包几百KB,百度网盘下载,再次感谢。
six-years 2012-12-20
  • 打赏
  • 举报
回复
引用 6 楼 caizz520 的回复:
引用 4 楼 Q1092926267 的回复:chart控件有所了解 兄弟 你贴这么多代码谁看啊 为了四十分 看个半天 跟你说 你这个直接设置X轴最大最小值 和间隔多少就可以了 Chart1.ChartAreas[0].AxisX.Interval = 1; 自己多试试 我刚学Chart所以我也不是很懂,呵呵 ……
以后有chart问题发帖给我发消息 一般问题还是能解决
VAllens 2012-12-20
  • 打赏
  • 举报
回复
引用 4 楼 Q1092926267 的回复:
chart控件有所了解 兄弟 你贴这么多代码谁看啊 为了四十分 看个半天 跟你说 你这个直接设置X轴最大最小值 和间隔多少就可以了 Chart1.ChartAreas[0].AxisX.Interval = 1; 自己多试试
我刚学Chart所以我也不是很懂,呵呵
VAllens 2012-12-20
  • 打赏
  • 举报
回复
引用 4 楼 Q1092926267 的回复:
chart控件有所了解 兄弟 你贴这么多代码谁看啊 为了四十分 看个半天 跟你说 你这个直接设置X轴最大最小值 和间隔多少就可以了 Chart1.ChartAreas[0].AxisX.Interval = 1; 自己多试试
在你未回复前就已经搞定了,自己看这个链接内帖子的结贴时间 http://q.cnblogs.com/q/44967/ 本来是想直接单击“未有满意答案结贴”的,但是这样结贴会扣40分,所以才等到你回复,然后选择满意答案结贴,这样系统会返回我20分。 不过还是谢谢你。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧