如何从分组条形图切换到简单条形图

How to switch from Grouped Bar chart to Simple Bar Charts

大家好,我最近试用了很棒的 iOS-Charts 库。

我制作了分组条形图,效果很好。我还有一个条形图也可以使用。

问题是,如果我从分组条形图切换到普通条形图,那么我的条形会变粗并放大。

但我无法缩小或更改条形的宽度。

This is the Normal bar how it should be

This is the Grouped bar chart

如果我从分组切换回正常 i get this

在切换下,我的意思是我通过创建新数据集来更新图表

代码如下所示:

func updateBarchart(which segment: Int)
{

    //getSumofMonths()


    let pecsColor = UIColor(red:0.278, green:0.247, blue:0.247, alpha: 1.000)
    let petreColor = UIColor(red:0.941, green:0.400, blue:0.184, alpha: 1.000)

    if segment == 1
    {
        let dataSets = [createDataSet(from: pecsBevetel, label: "Pécs", color: pecsColor),createDataSet(from: petreBevetel, label: "Újpetre", color: petreColor)]
        groupedBarChart.setGroupedBarchartData(from: dataSets, xValues: monthStrings)
        groupedBarChart.xAxis.granularityEnabled = true
        groupedBarChart.xAxis.granularity = groupedBarChart.xAxis.axisMaximum / Double(monthStrings.count)

        groupedBarChart.xAxis.axisMaximum = Double(12)
        groupedBarChart.setVisibleXRangeMaximum(6)
        groupedBarChart.xAxis.centerAxisLabelsEnabled = true

    }
    else if segment == 0
    {
        groupedBarChart.setBarChartData(xValues: monthStrings, yValues: pecsBevetel, label: "Pécs", colors: [pecsColor])




    }
    else if segment == 2
    {
        groupedBarChart.setBarChartData(xValues: monthStrings, yValues: petreBevetel, label: "Újpetre", colors: [petreColor])




    }

    //groupedBarChart.xAxis.axisMinimum = Double(-0.5)
    groupedBarChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
    groupedBarChart.chartDescription?.text = ""

    //groupedBarChart.fitBars = true

    groupedBarChart.doubleTapToZoomEnabled = false



    let legend = groupedBarChart.legend
    legend.enabled = true
    legend.horizontalAlignment = .center
    legend.verticalAlignment = .bottom
    legend.orientation = .horizontal
    legend.drawInside = false
    legend.yEntrySpace = 0.0;

    let  marker =  BalloonMarker(color: UIColor.white, font: UIFont.systemFont(ofSize: 12), textColor: UIColor.black, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
    marker.minimumSize = CGSize(width: 75.0, height: 35.0)
    groupedBarChart.marker = marker
    groupedBarChart.drawMarkers = true


}

使用这两个扩展更新数据

func setBarChartData(xValues: [String], yValues: [Double], label: String?, colors: [UIColor]?) {

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<yValues.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
    if let colours = colors
    {
        chartDataSet.colors = colours
    }
    let chartData = BarChartData(dataSet: chartDataSet)


    setxAxxis(with: xValues)

    chartData.barWidth = Double(0.85)


    self.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeOutCirc)

    self.data = chartData
}

func setxAxxis(with items: [String])
{
    let chartFormatter = BarChartFormatter(labels: items)
    let xAxis = XAxis()
    xAxis.valueFormatter = chartFormatter
    self.xAxis.valueFormatter = xAxis.valueFormatter
}

func setGroupedBarchartData(from entries: [BarChartDataSet], xValues: [String])
{
    let groupedChartData = BarChartData(dataSets: entries)
    setxAxxis(with: xValues)


    let groupSpace = 0.1
    let barSpace = 0.05
    let barWidth = 0.4

    groupedChartData.barWidth = barWidth
    groupedChartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)

    self.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeOutCirc)

    self.data = groupedChartData
}

请帮助我,因为 Google 几个小时后我无法让它工作 我正在考虑将其缩小,但没有用。或者我怎么能做一个干净的重绘,所以一切都设置回默认值,以便 barwidth 再次正常?

感谢您抽出时间来帮助我! :)

我遇到了同样的问题,经过数小时的研究,我得到了以下解决方案。

每次从组 => 正常或反之切换时,您都需要像下面这样重置和清零以前的数据设置。

重置旧数据集:

if barChartView.xAxis.isAxisMinCustom {
    barChartView.xAxis.resetCustomAxisMin()
}

if barChartView.xAxis.isAxisMaxCustom {
    barChartView.xAxis.resetCustomAxisMax()
}

//Remove all old Data Set from chart.
if ((barChartView.data?.dataSetCount)! > 0)
{
   barChartView.data = nil
}

通知数据已更改:

如果您在更新数据集后使用以下方法使用新数据更新数据。

barChartView.data?.notifyDataChanged()
barChartView.notifyDataSetChanged()

通过这样做,您将获得正确的条形预期宽度。

希望这会有所帮助。

我在文档中找到了答案。很难找到。 您唯一需要做的就是调用 fitScreen() 方法,然后它会恢复到原始状态。

func updateBarchart(which segment: Int)
{

    //getSumofMonths()

    let pecsColor = UIColor(red:0.278, green:0.247, blue:0.247, alpha: 1.000)
    let petreColor = UIColor(red:0.941, green:0.400, blue:0.184, alpha: 1.000)

    if segment == 1
    {
        let dataSets = [createDataSet(from: pecsBevetel, label: "Pécs", color: pecsColor),createDataSet(from: petreBevetel, label: "Újpetre", color: petreColor)]

        groupedBarChart.setGroupedBarchartData(from: dataSets, xValues: monthStrings)
        groupedBarChart.xAxis.granularityEnabled = true
        groupedBarChart.xAxis.granularity = groupedBarChart.xAxis.axisMaximum / Double(monthStrings.count)

        groupedBarChart.xAxis.axisMaximum = Double(12)
        groupedBarChart.setVisibleXRangeMaximum(6)
        //groupedBarChart.setVisibleXRangeMinimum(0)
        groupedBarChart.xAxis.centerAxisLabelsEnabled = true

    }
    else if segment == 0
    {
        groupedBarChart.setBarChartData(xValues: monthStrings, yValues: pecsBevetel, label: "Pécs", colors: [pecsColor])
        //groupedBarChart.setVisibleXRangeMinimum(12)
        groupedBarChart.xAxis.centerAxisLabelsEnabled = false
        groupedBarChart.fitScreen() //<- This method solves the problem.




    }
    else if segment == 2
    {
        groupedBarChart.setBarChartData(xValues: monthStrings, yValues: petreBevetel, label: "Újpetre", colors: [petreColor])
        //groupedBarChart.setVisibleXRangeMinimum(12)
        groupedBarChart.xAxis.centerAxisLabelsEnabled = false
        groupedBarChart.fitScreen() //<- This method solves the problem.

    }

    //groupedBarChart.xAxis.axisMinimum = Double(0.5)
    groupedBarChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
    groupedBarChart.chartDescription?.text = ""

    groupedBarChart.fitBars = true


    groupedBarChart.doubleTapToZoomEnabled = false


    let legend = groupedBarChart.legend
    legend.enabled = true
    legend.horizontalAlignment = .center
    legend.verticalAlignment = .bottom
    legend.orientation = .horizontal
    legend.drawInside = false
    legend.yEntrySpace = 0.0;

    let  marker =  BalloonMarker(color: UIColor.white, font: UIFont.systemFont(ofSize: 12), textColor: UIColor.black, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
    marker.minimumSize = CGSize(width: 75.0, height: 35.0)
    groupedBarChart.marker = marker
    groupedBarChart.drawMarkers = true
    groupedBarChart.moveViewToX(0)

}

简而言之,在设置图表数据后调用此方法。

groupedBarChart.fitScreen() //<- This method solves the problem.