iOS:两个条形图在一起
<p><p>我正在使用图表库(<a href="https://github.com/danielgindi/Charts" rel="noreferrer noopener nofollow">Charts</a>)</p>
<p>我正在开发的应用程序允许我实时显示餐厅的客人数量并比较不同日期之间的数据。
例如看这张图片
<a href="/image/wZPCB.png" rel="noreferrer noopener nofollow"><img src="/image/wZPCB.png" alt="enter image description here"/></a> </p>
<p>虚线表示比较日期的数据。我想像这样创建条形图,但库只允许我显示 4 个分组条。我想在带有小偏移的彩色虚线上方显示。请帮帮我</p>
<p> <a href="/image/zyMw1.png" rel="noreferrer noopener nofollow"><img src="/image/zyMw1.png" alt="enter image description here"/></a> </p>
<p><strong>我的代码是:</strong></p>
<pre><code>for (int i = 0; i < days.count; i++) {
BarChartDataEntry *guysEntry = [ initWithX:i y: integerValue]];
;
BarChartDataEntry *girlsEntry = [ initWithX:i y: integerValue]];
;
BarChartDataEntry *guysCompareToEntry = [ initWithX:i y: integerValue]];
;
BarChartDataEntry *girlsCompareToEntry = [ initWithX:i y: integerValue]];
;
}
BarChartDataSet *guysChartDataSet = [ initWithValues:guysChartDataArray label:@"Guys"];
guysChartDataSet.colors = @[];
guysChartDataSet.valueTextColor = ;
BarChartDataSet *girlsChartDataSet = [ initWithValues:girlsChartDataArray label:@"Girls"];
girlsChartDataSet.colors = @[];
girlsChartDataSet.valueTextColor = ;
LineChartXAxisFormatter *barGraphXFormatter = [ init];
barGraphXFormatter.xLabels = ;
self.barChartView.xAxis.valueFormatter = barGraphXFormatter;
self.barChartView.xAxis.centerAxisLabelsEnabled = YES;
self.combinedChartView.xAxis.valueFormatter = barGraphXFormatter;
self.combinedChartView.xAxis.centerAxisLabelsEnabled = YES;
float groupSpace = 0.06f;
float barSpace = 0.02f;
float barWidth = 0.45f;
BarChartDataSet *guysCompareToChartDataSet = [ initWithValues:guysCompareToChartDataArray label:@"Guys (Compare)"];
guysCompareToChartDataSet.colors = @[];
guysCompareToChartDataSet.barBorderWidth = 1.f;
guysCompareToChartDataSet.barBorderColor = ;
guysCompareToChartDataSet.isDashedBorder = YES;
guysCompareToChartDataSet.axisDependency = AxisDependencyLeft;
guysCompareToChartDataSet.valueTextColor = ;
BarChartDataSet *girlsCompareToChartDataSet = [ initWithValues:girlsCompareToChartDataArray label:@"Girls (Compare)"];
girlsCompareToChartDataSet.colors = @[];
girlsCompareToChartDataSet.barBorderWidth = 1.f;
girlsCompareToChartDataSet.barBorderColor = ;
girlsCompareToChartDataSet.isDashedBorder = YES;
girlsCompareToChartDataSet.axisDependency = AxisDependencyLeft;
girlsCompareToChartDataSet.valueTextColor = ;
NSArray *dataSets = @;
BarChartData *barChartData = [ initWithDataSets:dataSets];
barChartData.barWidth = barWidth;
CGFloat initialValue = 0;
CGFloat groupCount = days.count;
self.barChartView.xAxis.axisMinimum = initialValue;
self.barChartView.xAxis.axisMaximum = initialValue + * groupCount;
;
self.barChartView.data = barChartData;
</code></pre>
<p>我想做类似的东西:
<a href="/image/Yv5t5.png" rel="noreferrer noopener nofollow"><img src="/image/Yv5t5.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>既然你已经改变了你的问题,这就需要一个不同的解决方案。</p>
<p>正如我之前在评论中提到的,您需要手动计算每个条的位置。然后,<strong><em>不要</em></strong>使用分组功能,因为您已经按照自己的意愿对它们进行了分组。</p>
<pre><code>// Determine bar sizing and spacing parameters.
int barsPerGroup = 2;
double targetGroupWidth = 1;
double barSpacing = 0.2;
double groupSpacing = 0.3;
double barWidth = (targetGroupWidth - groupSpacing - barSpacing * barsPerGroup) / barsPerGroup;
double compareBarOffset = barWidth / 3;
for (int i = 0; i < entryCount; i++) {
// Determine X position for each bar
// NOTE: This is the most important step!
double groupStartPosition = targetGroupWidth * i;
double group1X = groupStartPosition + barWidth / 2;
double group1CompareX = group1X + compareBarOffset;
double group2X = group1X + barWidth + barSpacing;
double group2CompareX = group2X + compareBarOffset;
// Create data entries positioned at values calculated by previous step
NSNumber *group1Value = group1;
NSNumber *group1CompareValue = group1Compare;
NSNumber *group2Value = group2;
NSNumber *group2CompareValue = group2Compare;
BarChartDataEntry *group1DataEntry = [ initWithX:group1X
y:];
BarChartDataEntry *group1CompareDataEntry = [ initWithX:group1CompareX
y:];
BarChartDataEntry *group2DataEntry = [ initWithX:group2X
y:];
BarChartDataEntry *group2CompareEntry = [ initWithX:group2CompareX
y:];
// ...
}
// Create Data Sets, set styles, etc.
// ...
// Do NOT use this method because bars are already grouped.
//;
</code></pre>
<p><strong>结果:</strong></p>
<p>注意:我找不到属性 <code>isDashedBorder</code>,所以我不知道您使用的是什么版本的库。相反,我只是设置了带有纯灰色边框的清晰条。</p>
<p> <a href="/image/31eUN.png" rel="noreferrer noopener nofollow"><img src="/image/31eUN.png" alt="Chart with orange and blue bars with overlapping bars for comparison"/></a> </p></p>
<p style="font-size: 20px;">关于iOS:两个条形图在一起,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/50853671/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/50853671/
</a>
</p>
页:
[1]