MsChart固定X轴刻度数
Hi,请教大家一个问题.
我用了MsChart里的柱状图,数据是动态绑定的,而且是实时刷新,即有时绑定的数据点只有三个,有时又是四个,不固定的.
因为我把图表大小固定了,这样就造成了显示的柱形有时多有时少,而Mschart又自动的根据图表大小调整柱子的大小.有没有办
把X轴固定为十个刻度??
[解决办法]
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 010;
[解决办法]
/// <summary>
/// 画图
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="ar">要显示的指标</param>
/// <param name="title">标题</param>
/// <param name="filename">文件名</param>
public void DrawChart(DataTable dt, List<string> ar, string title, int widths, Boolean isShowInLabel, Boolean isShow3D, Control control, Boolean isLabelStyle, Boolean isClustered, string color, string filter,string ReqPage)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
for (int k = 0; k < dt.Rows.Count; k++)
{
//判断不是DBNUll
//判断被除的列不为空
if (dt.Rows[k][dt.Columns[j].ColumnName] == DBNull.Value)
{
dt.Rows[k][dt.Columns[j].ColumnName] = 0;
}
}
}
//Series.Point[i].ToolTip="#VALY"----(鼠标移上Point[i]时,显示Y值,移开时消失)
//声明一个chart图形
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
//设置chart的宽度
Chart1.Width = widths;
//设置chart的高度
Chart1.Height = 280;
Chart1.BackColor = Color.White;
//图片的呈现方式
Chart1.RenderType = RenderType.ImageTag;
//设置控件的颜色
//Chart1.Palette = ChartColorPalette.SemiTransparent;
//设置标题
// Title t = new Title(title, Docking.Top, new System.Drawing.Font("宋体", 11, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));//图片标题
//将标题加入到chart里面
//Chart1.Titles.Add(t);
//声明作图区
Chart1.ChartAreas.Add("Areas");
//使轴标签自动拟合
Chart1.ChartAreas["Areas"].AxisX.IsLabelAutoFit = true;
Chart1.ChartAreas["Areas"].BackColor = Color.White;
Chart1.ChartAreas["Areas"].Area3DStyle.WallWidth = 0;
//配置x轴的标签是否斜着显示,为了节约位置
if(isClustered==true)
{
Chart1.ChartAreas["Areas"].AxisX.Interval = 1;
Chart1.ChartAreas["Areas"].AxisX.IntervalOffset = 1;
Chart1.ChartAreas["Areas"].Area3DStyle.IsClustered = isClustered;
Chart1.ChartAreas["Areas"].AxisX.LabelStyle.Angle = -60;
}
//Chart1.ChartAreas["Areas"].Area3DStyle.PointGapDepth = 20;
//Chart1.ChartAreas["Areas"].Area3DStyle.Inclination = 0;
//Chart1.ChartAreas["Areas"].Area3DStyle.Rotation = 20;
//Chart1.ChartAreas["Areas"].Area3DStyle.IsClustered = true;
Chart1.ChartAreas["Areas"].BackGradientStyle = GradientStyle.TopBottom;
Chart1.ChartAreas["Areas"].BackSecondaryColor = Color.White;
Chart1.ChartAreas["Areas"].Area3DStyle.Enable3D = isShow3D;
Chart1.ChartAreas["Areas"].AxisX.MajorGrid.Enabled = false;
Chart1.ChartAreas["Areas"].AxisY.MajorGrid.Enabled = true;
if(filter!=string.Empty)
{
int maxnum=Convert.ToInt32(dt.Compute("max(" + filter + ")", ""));
int minnum=Convert.ToInt32(dt.Compute("min(" + filter + ")", ""));
int offset = maxnum - minnum / dt.Rows.Count;
if (minnum == 0)
{
Chart1.ChartAreas["Areas"].AxisY.Minimum = minnum;
}
else {
Chart1.ChartAreas["Areas"].AxisY.Minimum = minnum-2000;
}
Chart1.ChartAreas["Areas"].AxisY.Maximum = maxnum+2000;
}
if (isLabelStyle == true)
{
Chart1.ChartAreas["Areas"].AxisY.LabelStyle.Format = "P0";
}
//X,Y坐标线颜色和大小
//定轴式自动拟合
Chart1.ChartAreas["Areas"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont
[解决办法]
LabelAutoFitStyles.IncreaseFont
[解决办法]
LabelAutoFitStyles.WordWrap;
//绑定到数据表,并且选择x轴
//Chart1.DataBindTable(dt.DefaultView, "层次结构");
Chart1.DataSource = dt;
Chart1.DataBind();
string SeriesName=string.Empty;
for (int i = 0; i < ar.Count; i++)
{
SeriesName = ar[i].ToString().Split('_')[0];
string SeriesType = ar[i].ToString().Split('_')[1];
Series series = new Series(SeriesName);
Chart1.Series.Add(series);
if (SeriesType == "1")
{
Chart1.Series[SeriesName].ChartType = SeriesChartType.Column;
}
if (SeriesType == "2")
{
Chart1.Series[SeriesName].ChartType = SeriesChartType.Spline;
}
if (SeriesType == "3")
{
Chart1.Series[SeriesName].ChartType = SeriesChartType.Doughnut;
}
if (SeriesType == "4")
{
Chart1.Series[SeriesName].ChartType = SeriesChartType.Bar;
}
Chart1.Series[SeriesName].MarkerStyle = MarkerStyle.Circle;
Chart1.Series[SeriesName].MarkerSize =6;
Chart1.Series[SeriesName].MarkerColor = Color.RoyalBlue;
Chart1.Series[SeriesName].XValueMember = "层次结构";
Chart1.Series[SeriesName].YValueMembers = SeriesName;
Chart1.Series[SeriesName].CustomProperties = "DrawingStyle=Cylinder,PointWidth=0.3";
Chart1.Series[SeriesName].IsValueShownAsLabel = isShowInLabel;
if(isLabelStyle==true)
{
Chart1.Series[SeriesName].LabelFormat = "P";
}
// Chart1.Series[SeriesName].Color = Color.FromName("RoyalBlue");
}
if(color!=string.Empty)
{
Chart1.Series[0].Color = Color.FromName(color);
}
//Chart1.Series[1].Color = Color.Crimson;
//边框样式
// Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
//边框的颜色26, 59, 105
Chart1.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
//边框线的样式
Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
//线的宽度
Chart1.BorderWidth = 2;
//加图例
Chart1.Legends.Add("Legend1");
//是否显示
Chart1.Legends["Legend1"].Enabled = true;
Chart1.Legends["Legend1"].BorderColor = Color.Gray;
Chart1.Legends["Legend1"].BorderWidth = 1;
//Chart1.Legends["Legend1"].DockedToChartArea = "Areas";
//Chart1.Legends["Legend1"].Docking=Docking.Left;
//Chart1.Legends["Legend1"].LegendStyle =LegendStyle.Table;
Chart1.Legends["Legend1"].Docking = System.Web.UI.DataVisualization.Charting.Docking.Bottom;
// Chart1.Page = Page;
//将控件加入到Panel容器中
foreach (Series series in Chart1.Series)
{
series.ToolTip = series.Name + ":#VALY";
if (ReqPage == "跃迁客户详细")
{
string dateCombo=this.comboDate.SelectedItem.Value;
#region 代码
series.MapAreaAttributes = "onclick="javascript:getReq('#VALX','#VALY','Webs/YueQianReportDetail.aspx?comb2=" + dateCombo + "&comb1=" + this.combo.SelectedItem.Value + "&xval=#VALX','跃迁客户分析','901');"";
}
if (ReqPage == "存款分析")
{
series.MapAreaAttributes = "onclick="javascript:getReq('#VALX','#VALY','Webs/DepositReport.aspx?xval=#VALX','存款分机构分析','902');"";
}
if (ReqPage == "金融资产分析")
{
series.MapAreaAttributes = "onclick="javascript:getReq('#VALX','#VALY','Webs/AssetsReport.aspx?xval=#VALX','金融资产分机构分析','903');"";
}
if (series.Name == "个人有效客户数")
{
series.MapAreaAttributes = "onclick="javascript:getReq('#VALX','#VALY','Webs/CustReport.aspx?xval=#VALX','有效客户数分机构分析','904');"";
}
#endregion
}
control.Controls.Add(Chart1);
control.Update();
}
楼主拿去修修改改,前台不需要代码
[解决办法]
private void getDaysTrafficByUsername(string userName,string client,DateTime start,DateTime end)
{
DataTable dt = new DataTable();
dt.Columns.Add("days"); //改成候选人的列名
dt.Columns.Add("daysTrafficSum"); //候选人人气的列 如果没有就count(*)as
daysTrafficSum 这样就能把每个人显示在x上并且把每个人的人气数量显示在Y上了 以前写的没问题 有什么就发帖吧
dt = omaEntity.daysTrafficSum(); //调用业务层的方法
//设置图表的数据源
dayChart.DataSource = dt;
//设置图表Y轴对应项
dayChart.Series[0].XValueMember = "days"; //改成候选人的列名
dayChart.Series[0].YValueMembers = "daysTrafficSum"; //候选人人气的列
dayChart.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval = 1;
dayChart.ChartAreas["ChartArea1"].AxisX.LabelStyle.Interval = 1;
dayChart.SaveImage("D:/img/"+userName +"_Day_"+client+".jpeg",System.Drawing.Imaging.ImageFormat.Jpeg);//图片保存的目录
dayChart.DataBind();
}