Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart interactions #72

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RNiOSCharts/RNHorizontalBarChart.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// RNHorizontalBarChart.m
//
//
//
// Created by Jose Padilla on 1/29/16.
//
Expand Down
66 changes: 33 additions & 33 deletions RNiOSCharts/RNHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,114 +16,114 @@ class RNHorizontalBarChart : HorizontalBarChartView {
super.init(frame: frame);
self.frame = frame;
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented");
}

func setConfig(_ config: String!) {
setBarLineChartViewBaseProps(config);

var maximumDecimalPlaces: Int = 0;
var minimumDecimalPlaces: Int = 0;
var labels: [String] = [];

var json: JSON = nil;
if let data = config.data(using: String.Encoding.utf8) {
json = JSON(data: data);
};

if json["labels"].exists() {
labels = json["labels"].arrayValue.map({$0.stringValue});
}

if json["dataSets"].exists() {
let dataSets = json["dataSets"].arrayObject;

var sets: [BarChartDataSet] = [];

for set in dataSets! {
let tmp = JSON(set);
if tmp["values"].exists() {
let values = tmp["values"].arrayValue.map({$0.doubleValue});
let label = tmp["label"].exists() ? tmp["label"].stringValue : "";
var dataEntries: [BarChartDataEntry] = [];

for i in 0..<values.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i);
dataEntries.append(dataEntry);
}

let dataSet = BarChartDataSet(yVals: dataEntries, label: label);

if tmp["barShadowColor"].exists() {
dataSet.barShadowColor = RCTConvert.uiColor(tmp["barShadowColor"].intValue);
}

if tmp["barSpace"].exists() {
dataSet.barSpace = CGFloat(tmp["barSpace"].floatValue);
}

if tmp["highlightAlpha"].exists() {
dataSet.highlightAlpha = CGFloat(tmp["highlightAlpha"].floatValue);
}

if tmp["highlightColor"].exists() {
dataSet.highlightColor = RCTConvert.uiColor(tmp["highlightColor"].intValue);
}

if tmp["highlightLineDashLengths"].exists() {
dataSet.highlightLineDashLengths = [CGFloat(tmp["highlightLineDashLengths"].floatValue)];
}

if tmp["highlightLineDashPhase"].exists() {
dataSet.highlightLineDashPhase = CGFloat(tmp["highlightLineDashPhase"].floatValue);
}

if tmp["highlightLineWidth"].exists() {
dataSet.highlightLineWidth = CGFloat(tmp["highlightLineWidth"].floatValue);
}

if tmp["stackLabels"].exists() {
dataSet.stackLabels = tmp["stackLabels"].arrayValue.map({$0.stringValue});
}

if tmp["colors"].exists() {
let arrColors = tmp["colors"].arrayValue.map({$0.intValue});
dataSet.colors = arrColors.map({return RCTConvert.uiColor($0)});
}

if tmp["drawValues"].exists() {
dataSet.drawValuesEnabled = tmp["drawValues"].boolValue;
}

if tmp["highlightEnabled"].exists() {
dataSet.highlightEnabled = tmp["highlightEnabled"].boolValue;
}

if tmp["valueTextFontName"].exists() {
dataSet.valueFont = UIFont(
name: tmp["valueTextFontName"].stringValue,
size: dataSet.valueFont.pointSize
)!;
}

if tmp["valueTextFontSize"].exists() {
dataSet.valueFont = dataSet.valueFont.withSize(CGFloat(tmp["valueTextFontSize"].floatValue))
}

if tmp["valueTextColor"].exists() {
dataSet.valueTextColor = RCTConvert.uiColor(tmp["valueTextColor"].intValue);
}

if json["valueFormatter"].exists() {
if json["valueFormatter"]["minimumDecimalPlaces"].exists() {
minimumDecimalPlaces = json["valueFormatter"]["minimumDecimalPlaces"].intValue;
}
if json["valueFormatter"]["maximumDecimalPlaces"].exists() {
maximumDecimalPlaces = json["valueFormatter"]["maximumDecimalPlaces"].intValue;
}

if json["valueFormatter"]["type"].exists() {
switch(json["valueFormatter"]["type"]) {
case "regular":
Expand All @@ -136,7 +136,7 @@ class RNHorizontalBarChart : HorizontalBarChartView {
dataSet.valueFormatter = NumberFormatter();
}
}

if json["valueFormatter"]["numberStyle"].exists() {
switch(json["valueFormatter"]["numberStyle"]) {
case "CurrencyAccountingStyle":
Expand Down Expand Up @@ -181,27 +181,27 @@ class RNHorizontalBarChart : HorizontalBarChartView {
dataSet.valueFormatter?.numberStyle = .none;
}
}

dataSet.valueFormatter?.minimumFractionDigits = minimumDecimalPlaces;
dataSet.valueFormatter?.maximumFractionDigits = maximumDecimalPlaces;
}

sets.append(dataSet);
}
}

let chartData = BarChartData(xVals: labels, dataSets: sets);
self.data = chartData;
}

if json["drawValueAboveBar"].exists() {
self.drawValueAboveBarEnabled = json["drawValueAboveBar"].boolValue;
}

if json["drawHighlightArrow"].exists() {
self.drawHighlightArrowEnabled = json["drawHighlightArrow"].boolValue;
}

if json["drawBarShadow"].exists() {
self.drawBarShadowEnabled = json["drawBarShadow"].boolValue;
}
Expand Down
1 change: 1 addition & 0 deletions RNiOSCharts/RNLineChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@interface RNLineChart : RCTView

@property (nonatomic, assign) NSString *config;
@property (nonatomic, copy) RCTBubblingEventBlock onSelect;

@end
3 changes: 2 additions & 1 deletion RNiOSCharts/RNLineChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@interface RCT_EXTERN_MODULE(RNLineChartSwift, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(config, NSString);
RCT_EXPORT_VIEW_PROPERTY(onSelect, RCTBubblingEventBlock);
RCT_EXTERN_METHOD(setVisibleXRangeMaximum:(nonnull NSNumber *)reactTag value:(CGFloat *)v);

@end
@end
50 changes: 39 additions & 11 deletions RNiOSCharts/RNLineChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,69 @@ import SwiftyJSON


@objc(RNLineChart)
class RNLineChart : LineChartView {

class RNLineChart : LineChartView, ChartViewDelegate {
var selectCallback : RCTBubblingEventBlock?

override init(frame: CGRect) {
super.init(frame: frame);
self.frame = frame;
self.delegate = self;
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented");
}

func setConfig(_ config: String!) {
self.descriptionText = "";

setBarLineChartViewBaseProps(config);

var labels: [String] = [];

var json: JSON = nil;
if let data = config.data(using: String.Encoding.utf8) {
json = JSON(data: data);
};

if json["labels"].exists() {
labels = json["labels"].arrayValue.map({$0.stringValue});
}

self.data = getLineData(labels, json: json);

if json["drawMarkers"].exists() {
self.drawMarkers = json["drawMarkers"].boolValue;
}

if json["leftAxis"]["startAtZero"].exists() {
self.leftAxis.startAtZeroEnabled = json["leftAxis"]["startAtZero"].boolValue;
}
}

func setOnSelect(_ onSelect: @escaping RCTBubblingEventBlock) {
selectCallback = onSelect;
}

// MARK: ChartViewDelagate
func chartValueSelected(_ chartView: BarLineChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
NSLog("chart element selected");
if (selectCallback == nil) {
return;
}
selectCallback!([
"xIndex": entry.xIndex,
"yValue": entry.value
]);
}

func chartValueNothingSelected(_ chartView: ChartViewBase) {
NSLog("chart element unselected");
if (selectCallback == nil) {
return;
}
selectCallback!([
"xIndex": -1,
]);
}
}
Loading