PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.02k forks source link

my pie chart legend still not showing can anyone help me please #2609

Open nbnitin opened 7 years ago

nbnitin commented 7 years ago

override func viewDidLoad() { super.viewDidLoad() // setLeftChart() distance = CGFloat(0.0) setRightChart()

   let months : [String] = ["Jan", "feb"]
    let unitsSold = [25.0, 20.0]
     pieChart.delegate = self
    pieChart.legend.enabled = true
    let legend = pieChart.legend

    legend.horizontalAlignment = .center

    legend.drawInside = true   // can be true
    legend.xEntrySpace = 20.0
    legend.yEntrySpace = 20.0
    legend.yOffset = 0.0
    legend.textColor = UIColor.purple
    let enteries: [LegendEntry] = [LegendEntry(label: "Jan", form: .square, formSize: 4.0, formLineWidth: 10.0, formLineDashPhase: 0.0, formLineDashLengths: nil, formColor: UIColor.red), LegendEntry(label: "Feb", form: .square, formSize: 4.0, formLineWidth: 10.0, formLineDashPhase: 0.0, formLineDashLengths: nil, formColor: UIColor.orange)]
    legend.entries = enteries
    pieChart.legend.enabled = true
    setChart(months, unitsSold)

}

func setChart( dataPoints: [String], values: [Double]) {

    var dataEntries: [ChartDataEntry] = []

    //    ChartLegend *l = _chartView.legend;
    //    l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    //    l.verticalAlignment = ChartLegendVerticalAlignmentTop;
    //    l.orientation = ChartLegendOrientationVertical;
    //    l.drawInside = NO;
    //    l.xEntrySpace = 7.0;
    //    l.yEntrySpace = 0.0;
    //    l.yOffset = 0.0;

    for i in 0..<dataPoints.count {
        let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i])

        dataEntries.append(dataEntry1)

    }

    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "hello")

    pieChartDataSet.sliceSpace = 2

    pieChartDataSet.yValuePosition = .outsideSlice

    let text = "Total \n Rs 100000"

    let style = NSMutableParagraphStyle()
    style.alignment = .center
    let attributes = [

        NSParagraphStyleAttributeName: style
    ]

    let range = (text as NSString).range(of: "Total")

    let attribute = NSMutableAttributedString.init(string: text)
    attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGray , range: range)
    attribute.addAttributes(attributes, range: range)

    pieChart.centerAttributedText = attribute
    pieChart.holeRadiusPercent = 0.6
    pieChart.chartDescription?.enabled = true

    var colors: [UIColor] = []
    let color1 = UIColor.blue
    colors.append(color1)

    let color2 = UIColor.brown
    colors.append(color2)
    pieChartDataSet.colors = colors

    pieChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

    let pieChartData = PieChartData(dataSet: pieChartDataSet)
    pieChart.data = pieChartData
}
GoneUp commented 7 years ago

Wrong Project. You need to go to https://github.com/danielgindi/Charts