Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Created January 15, 2013 19:22
Show Gist options
  • Save alexrothenberg/4541222 to your computer and use it in GitHub Desktop.
Save alexrothenberg/4541222 to your computer and use it in GitHub Desktop.
class MyController < UIViewController
def viewDidLoad
super
top_view.addSubview chart_view([[0,0], [100,100],
[10, 20, 20, 15, 35],
["#203a65", "#37537c", "#5b7faa", "#aec2d6", "#aec2d6", "#ffffff"]
)
end
def chart_view(frame, data, colors)
chart_view = CPTGraphHostingView.alloc.initWithFrame frame
chart_view.backgroundColor = UIColor.clearColor
chart = PieChart.alloc.initWithData(data, colors: colors, andSize: chart_view.frame.size)
chart_view.hostedGraph = chart
chart_view
end
end
class PieChart < CPTXYGraph
attr_reader :data, :size, :colors
def initWithData(data, colors: colors, andSize: size)
init
@data = data
@size = size
@colors = colors || []
plot.pieRadius = radius
plot.identifier = 'this is a chart'
plot.startAngle = Math::PI/2
plot.sliceDirection = CPTPieDirectionCounterClockwise
plot.dataSource = self;
plot.delegate = self;
addPlot plot, toPlotSpace: defaultPlotSpace
self
end
def radius
[ 0.9 * (size.first - 2 * paddingLeft) / 2.0,
0.9 * (size.last - 2 * paddingTop ) / 2.0
].min
end
def numberOfRecordsForPlot(plot)
data.size
end
def numberForPlot(plot, field:fieldEnum, recordIndex:index)
data[index]
end
def sliceFillForPieChart(plot, recordIndex: index)
color = if colors[index]
String.new(colors[index]).to_color.asCPTColor
else
CPTPieChart.defaultPieSliceColorForIndex(index)
end
CPTFill.alloc.initWithColor color
end
def plot
@plot ||= CPTPieChart.alloc.init
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment