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

时间:2018-07-08 02:10:34

标签: ios swift bar-chart ios-charts

大家好,我最近尝试了很棒的iOS图表库。

我制作了分组条形图,其工作正常。我也有一个条形图也可以。

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

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

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数小时后,我无法正常使用 我当时正在考虑将其缩小,但是没有用。或者我该如何进行干净的重绘,以便将所有内容都设置为默认值,以使条码宽度再次正常?

如果您能帮助我,谢谢您的时间! :)

2 个答案:

答案 0 :(得分:2)

我在文档中找到了答案。很难找到。 您唯一要做的就是调用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.

答案 1 :(得分:0)

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

每次从组=>正常切换时,您都需要重置和取消以前的数据设置,如下所示。

重置旧数据集:

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()

这样做,您将获得适当的期望宽度。

希望这会有所帮助。