你好,我的需求是:画柱状图时,根据不同范围的值,柱状图显示不同颜色
柱状图是不叠加
尝试了几种方法,还是有不少问题,最后一次代码如下:
for (int i = 0; i < type.Count; i++)
{
StackedBarSeries barSeries = new StackedBarSeries();
barSeries.YAxisId = 1;
//柱状图 绿色
BarSeries barGreen = new BarSeries();
barGreen.YAxisId = 0;
barGreen.LabelVisible = true;
//柱宽度
barGreen.GapWidth = 1F;
var fillGreen = new FarPoint.Win.Chart.SolidFill(Color.Green);
barGreen.BarFill = fillGreen;
//柱状图 绿色
BarSeries barYellow = new BarSeries();
barYellow.YAxisId = 0;
barYellow.LabelVisible = true;
//柱宽度
barYellow.GapWidth = 1F;
var fillYellow = new FarPoint.Win.Chart.SolidFill(Color.Yellow);
barYellow.BarFill = fillYellow;
//柱状图 绿色
BarSeries barRed = new BarSeries();
barRed.YAxisId = 0;
barRed.LabelVisible = true;
//柱宽度
barRed.GapWidth = 1F;
var fillRed = new FarPoint.Win.Chart.SolidFill(Color.Red);
barRed.BarFill = fillRed;
//最大工作时间折线图
LineSeries maxLine = new LineSeries();
maxLine.PointMarker = new BuiltinMarker(MarkerShape.Diamond, 2.0F);
maxLine.YAxisId = 0;
maxLine.LabelVisible = false;
//折线颜色、宽度
maxLine.LineBorder = new SolidLine(Color.Red, 1.5F);
//定时工作时间折线图
LineSeries stdLine = new LineSeries();
stdLine.PointMarker = new BuiltinMarker(MarkerShape.Diamond, 2.0f);
stdLine.YAxisId = 0;
stdLine.LabelVisible = false;
stdLine.LineBorder = new SolidLine(Color.Orange, 1.5f);
for (int x = 2; x < _myTblData.Columns.Count; x++)
{
var dc = _myTblData.Columns[x];
var that = data.FirstOrDefault(s => s.WORK_DATE == Convert.ToDateTime(dc.ColumnName) && s.DATA_TYPE == type[i]);
if (that != null)
{
if(that.WORK_TIME_ACT<that.WORK_TIME_STD)
{
barGreen.Values.Add(Convert.ToDouble(ToHours(that.WORK_TIME_ACT)));
barYellow.Values.Add(0);
barRed.Values.Add(0);
}
if (that.WORK_TIME_ACT > that.WORK_TIME_STD && that.WORK_TIME_ACT<that.WORK_TIME_MAX)
{
barYellow.Values.Add(Convert.ToDouble(ToHours(that.WORK_TIME_ACT)));
barGreen.Values.Add(0);
barRed.Values.Add(0);
}
if(that.WORK_TIME_ACT>that.WORK_TIME_MAX)
{
barRed.Values.Add(Convert.ToDouble(ToHours(that.WORK_TIME_ACT)));
barGreen.Values.Add(0);
barYellow.Values.Add(0);
}
stdLine.Values.Add(Convert.ToDouble(ToHours(that.WORK_TIME_STD)));
maxLine.Values.Add(Convert.ToDouble(ToHours(that.WORK_TIME_MAX)));
}
else
{
barGreen.Values.Add(0);
barYellow.Values.Add(0);
barRed.Values.Add(0);
stdLine.Values.Add(0);
maxLine.Values.Add(0);
}
}
barSeries.Series.Add(barGreen);
barSeries.Series.Add(barYellow);
barSeries.Series.Add(barRed);
//折线图设置
ValueAxis axisConfig = new ValueAxis();
axisConfig.AxisId = 1;
axisConfig.AutoMaximum = true;
axisConfig.AutoMinimum = true;
//隐藏折线图Y轴
axisConfig.LabelVisible = false;
axisConfig.Location = AxisLocation.Far;
//Y绘图区
YPlotArea plotArea = new YPlotArea();
plotArea.Location = new PointF(0, 0.09F);
plotArea.Size = new SizeF(1, 0.9F);
plotArea.Series.Add(barSeries);
//plotArea.Series.Add(barYellow);
//plotArea.Series.Add(barRed);
plotArea.Series.Add(stdLine);
plotArea.Series.Add(maxLine);
//隐藏左边Y轴标尺
plotArea.YAxes[0].RulerLine = new NoLine();
//隐藏X轴下的数字
plotArea.XAxis.LabelVisible = false;
plotArea.XAxis.RulerLine = new NoLine();
//隐藏柱状图Y轴数字
plotArea.YAxes[0].LabelVisible = false;
plotArea.YAxes.Add(axisConfig);
//隐藏网格线
plotArea.YAxes[0].MajorGridLine = new NoLine();
plotArea.YAxes[1].MajorGridLine = new NoLine();
ChartModel model = new ChartModel();
model.PlotAreas.Add(plotArea);
var chart = new FarPoint.Win.Spread.Chart.SpreadChart();
chart.CanMove = FarPoint.Win.Spread.DrawingSpace.Moving.None;
chart.CanSize = FarPoint.Win.Spread.DrawingSpace.Sizing.None;
chart.Size = new Size((_mySheet.ColumnCount - 2) * _dateColWidth, _spanRowHeight * 4);
int chartY = Convert.ToInt32((_oneAreaMiniRows + type.Count()) * _rowHeihgt) + (_oneAreaMiniRows + type.Count) * 2 + 2;
if (i > 0)
{
chartY = chartY + Convert.ToInt32(_spanRowHeight * (i * 4));
}
//数据类型作业区微调
if (type.Count == 1)
chartY += 2;
if (type.Count == 4)
chartY -= 4;
if (type.Count == 3)
chartY -= 2;
chart.Location = new Point(_wsCdColWidth + _titleColWidth, chartY);
chart.Model = model;
_mySpread.ActiveSheet.Charts.Add(chart);
}
|
|