2017-01-30 4 views
1

私は現在、このようになります棒グラフを持っています。Swiftを使用して、このiOSバーチャートに白と14ピクセルの値を追加する方法は?私は白でバーの最後に各バーの値を入れたい</p> <p><a href="https://i.stack.imgur.com/6uPx0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6uPx0.png" alt="enter image description here"></a></p> <p>:

これは私の現在のコードです:

viewDidLoad{ 
self.results = ["Lost", "Drawn", "Won"] 
let games = [total_Losses, total_Draws, total_Wins] 
self.setChart(dataPoints: self.results, values: games) 
} 


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

    let formato:BarChartFormatter = BarChartFormatter() 
    let xaxis:XAxis = XAxis() 
    let xAxis : XAxis = self.barChartView.xAxis; 
    barChartView.noDataText = "you need to provide some data for the chart." 

    var dataEntries: [BarChartDataEntry] = Array() 

    for i in 0..<dataPoints.count { 
     let dataEntry = BarChartDataEntry(x: Double(i), y: values[i]) 
     dataEntries.append(dataEntry) 
     formato.stringForValue (Double(i), axis: xaxis) 
    } 

    xaxis.valueFormatter = formato 
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter 

    xAxis.labelFont = UIFont(name: "Avenir", size: 14.0)! 
    xAxis.labelTextColor = UIColor.white 

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played") 
    let chartData = BarChartData(dataSets: [chartDataSet]) 
    chartDataSet.colors = ChartColorTemplates.material() 


    barChartView.leftAxis.enabled = true 
    barChartView.legend.enabled = false 
    barChartView.chartDescription?.text = "" 

    chartData.addDataSet(chartDataSet) 
    barChartView.data = chartData 
    barChartView.leftAxis.forceLabelsEnabled = true 
    barChartView.rightAxis.forceLabelsEnabled = true 

    barChartView.xAxis.granularityEnabled = true 
    barChartView.xAxis.granularity = 1.0 

    barChartView.leftAxis.drawGridLinesEnabled = false 
    barChartView.rightAxis.drawGridLinesEnabled = false 
    barChartView.xAxis.drawGridLinesEnabled = false 

    barChartView.rightAxis.enabled = false 
    barChartView.drawGridBackgroundEnabled = false 
    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom 

} 

私は白とピクセルサイズ14で、このチャートにバーに値を追加するにはどうすればよいですか?

の答えは、これまで動作するように失敗している - 私はこの問題は、内にあるかもしれないと思う:コードのこの上記のスニペットは、以下のこのクラスを呼び出す

for i in 0..<dataPoints.count { 
     let dataEntry = BarChartDataEntry(x: Double(i), y: values[i]) 
     dataEntries.append(dataEntry) 
     formato.stringForValue (Double(i), axis: xaxis) // This line has a warning: "Result of call to 'stringForValue(_:axis:)' is unused 
    } 

あなたが設定することができます
public class BarChartFormatter: NSObject, IAxisValueFormatter 
{ 
var matches: [String]! = ["Lost", "Drawn", "Won"] 

public func stringForValue(_ value: Double, axis: AxisBase?) -> String 
{ 
    return matches[Int(value)] 
} 
} 
+0

x軸のラベルを有効にしたり、フォントや色を設定することができます。 – sdasdadas

答えて

0

色とフォントはBarChartDataクラスオブジェクトを使用しています。あなたの場合は、chartData

ラベルの必要なフォントと色を設定するために、次のコードに従ってください。場合

chartData.setValueFont(UIFont.systemFont(ofSize: 14.0)) 
chartData.setValueTextColor(UIColor.white) 

あなたはfalseにごBarChartViewオブジェクトのプロパティdrawValueAboveBarEnabledを設定し、その後バー内のラベルを表示したいです。バーの中にラベルが表示されます。

barChartView.drawValueAboveBarEnabled = false 
+0

残念ながらそれはうまくいきませんでした - 問題は他の場所にあると思いますか?上記の質問を更新しました。おかげで – RDowns

0

スウィフトでは、各バーの値を設定して、次に示すコードを実装することができます。

barChartView.data?.setDrawValues(true) 
    barChartView.data?.setValueTextColor(UIColor.white) 
    barChartView.data?.setValueFont(UIFont.systemFont(ofSize: 14.0)) 

あなたの問題を解決したいと考えています。

+0

残念ながらそれはうまくいきませんでした - 私は問題が他の場所にあると思う?上記の質問を更新しました。ありがとう – RDowns

+0

どの軸で値を設定しますか? –

+0

私はそれらを上記の画像の右側にあるように、各バーの上部に表示します。 – RDowns

関連する問題

 関連する問題