[英]iOS-Charts - Make a bar chart and field remaning with other color
我正在嘗試制作與設計人員指南完全相同的圖表,但我在這三件事上遇到了困難。
這是指導方針:
這就是我所做的:
如您所見,假定紅色條在自身和目標之間有一個藍色條。 已經嘗試創建第二個欄,以查看是否仍在后面,但這不起作用。 這就是我嘗試過的
BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithYVals:yVals label:@""];
然后提起藍色標靶。 但是沒有用,所以我已經刪除了。
另一個問題是左軸標簽從零開始,並且確實找到了解決方法。
第三個也是最后一個問題是,當預期值位於行之后時,目標的T會保持在行上方。
這是用於獲取圖表的代碼:
/*
* Method to render the graphic of a given KPI
*
* @param chartView BarChartView object type
* @param array NSMutableArray with array of dictionaries
* @param target double with target value
* @param granularity NSString ("Monthly", "weekly" or "daily")
*
* @return id BarCartView
*/
- (id)graphicRenderIn:(BarChartView*)chartView
withData:(NSMutableArray*)array
target:(double) target
andGranularity:(NSString*)granularity{
_chartView = chartView;
[_chartView setUserInteractionEnabled:NO];
// chartView setup
_chartView.delegate = self;
_chartView.descriptionText = @"";
_chartView.noDataTextDescription = @"You need to provide data for the chart.";
_chartView.maxVisibleValueCount = 60;
_chartView.pinchZoomEnabled = NO;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawGridBackgroundEnabled = NO;
_chartView.backgroundColor = [UIColor green_title];
// Ox axis setup
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.spaceBetweenLabels = 0.0;
xAxis.drawGridLinesEnabled = NO;
xAxis.axisLineColor = [UIColor turqoise];
xAxis.labelTextColor = [UIColor white_100];
xAxis.labelFont = [UIFont Caption1];
_chartView.leftAxis.drawGridLinesEnabled = NO;
_chartView.rightAxis.drawGridLinesEnabled = NO;
_chartView.rightAxis.enabled = NO;
_chartView.legend.enabled = NO;
// Oy axis setup
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont Caption1];
leftAxis.labelTextColor = [UIColor white_100];
leftAxis.labelCount = 3;
leftAxis.labelPosition = YAxisLabelPositionInsideChart;
leftAxis.spaceTop = 0.15;
leftAxis.axisLineColor = [UIColor turqoise];
// define TARGET line in graphic
[leftAxis removeAllLimitLines];
[self updateTargetLine:target];
[leftAxis addLimitLine:_line];
// set data values
[self setDataCount:array target:target andGranularity:granularity];
[_chartView animateWithYAxisDuration:2.5];
return self;
}
/*
* Method to setup the horizontal target line
*
* @param target double with target value
*
* @return void
*/
- (void) updateTargetLine:(double) target
{
// single target line
if (_line == nil)
{
ChartLimitLine *targetLine = [[ChartLimitLine alloc] initWithLimit:target label:@""];
_line = targetLine;
}
// target line setup
if (target != 0 )
{
_line.label = @"T";
}
_line.lineWidth = 0.8;
_line.lineColor = [UIColor app_blue];
_line.labelPosition = ChartLimitLabelPositionRight;
_line.valueFont = [UIFont systemFontOfSize:10.0];
}
/*
* Method to setup data for the BarChart graphic
*
* @param array NSMutableArray with array of dictionaries
* @param target double with target value
* @param granularity NSString ("Monthly", "weekly" or "daily")
*
* @return void
*/
- (void)setDataCount:(NSMutableArray*)array target:(double)target andGranularity:(NSString *)granularity
{
NSInteger arraySize = [array count];
NSMutableArray *arrayOy = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *arrayOx = [[NSMutableArray alloc] initWithCapacity:0];
// get array of values(arrayOy) and keys(arrayOx) from
for (NSDictionary *elem in array) {
[arrayOy addObject: [elem objectForKey:@"value" ]];
[arrayOx addObject:[elem objectForKey:@"date"]];
}
NSMutableArray *yVals = [[NSMutableArray alloc] init];
NSMutableArray *arrColor = [[NSMutableArray alloc] init];
for(int i = 0; i < arraySize; i++)
{
double val = [[arrayOy objectAtIndex:i]doubleValue];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i+1]];
if (val < target ) {
[arrColor addObject:[UIColor red_bar]];
}else{
[arrColor addObject:[UIColor green_bar]];
}
}
// Ox label values depends on granularity
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:@""]; // empty value to creat padding from the end
for (int i = 0; i < arraySize; i++)
{
// Formatting Ox axis labels concerning data and granularity
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[[arrayOx objectAtIndex:i] doubleValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if ([granularity isEqualToString:@"dayly"] ) {
[formatter setDateFormat:@"HH:mm"];
}else{
[formatter setDateFormat:@"dd/MM"];
}
[xVals addObject:[formatter stringFromDate:date]];
}
[xVals addObject:@""]; // empty value to creat padding from the end
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@""];
[set1 setColors:(arrColor)];
[set1 setBarSpace: 0.90]; // creat a space between of each bar 90%
set1.drawValuesEnabled = NO;
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
_chartView.data = data;
}
您的ios圖表版本是什么? 我猜當您突出顯示紅色條時,藍色條在那里? 如果僅當您在最新版本中突出顯示它時,才能解決該問題。
對於Q2,每個yAxis都有一個名為startAtZeroEnabled
的屬性。 做任何你喜歡的事。
對於第三季度,在最新代碼中,限制線標簽具有更多位置選項。 另一種方法是您可以嘗試在yAxis渲染器的renderLimitLine函數中編寫自己的位置
這就是我用於圖表的內容。 您的回答對我很有幫助!
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart"
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
barChartView.descriptionText = ""
chartDataSet.colors = [UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawLabelsEnabled = false
barChartView.xAxis.labelWidth = 3
barChartView.xAxis.axisLineWidth = 1
barChartView.xAxis.labelTextColor = UIColor.whiteColor()
barChartView.xAxis.axisLineColor = UIColor.whiteColor()
barChartView.xAxis.labelPosition = .Bottom
barChartView.rightAxis.enabled = false
//barChartView.xAxis.labelTextColor = UIColor.whiteColor()
barChartView.drawBarShadowEnabled = false
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.leftAxis.enabled = true
barChartView.leftAxis.axisLineColor = UIColor.whiteColor()
barChartView.leftAxis.labelTextColor = UIColor.whiteColor()
barChartView.leftAxis.labelCount = 4
barChartView.leftAxis.axisLineWidth = 1
barChartView.legend.enabled = false
//barChartView.borderLineWidth
}
這就是我得到的: https : //www.dropbox.com/s/uskvwcbq3mlaf9o/Screen%20Shot%202016-04-25%20at%2017.14.36.png?dl=0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.