core-plot swift version of barFillForPlotColor - core-plot

Good day to all of you;
I have recently started working with CorePlot in my iOS projects and have learned how to create bar charts from my data (thank you RayWenderlich tutorial).
Now, rather than using one colour for all the bars I would like to colour them based on their values. For example, values below 1.0 are red, values of 1.0 are blue, and values above 1.0 are green.
I know this can be done and I have read numerous articles that talk about using the -(nullable CPTFill *)barFillForBarPlot:(nonnull CPTBarPlot *)barPlot recordIndex:(NSUInteger)index function. In fact I have looked at the coloredBarChart example in the CorePlot Examples - Plot Gallery and see how they are using this function. The problem I am having is that I am programming in Swift and need to have a Swift version of this function that I can use, or an explanation of how I can use this version in my Swift code.
Any assistance that you can provide is greatly appreciated.

The Swift compiler automatically renames Objective-C methods to make them more "Swifty". Try this in the bar plot datasource:
func barFill(for plot: CPTBarPlot, record: UInt) -> CPTFill?
{
return CPTFill(color: .red()) // or whatever color you want based on the data value
}

Ah, I see; the function call gets converted to be Swift like. Based on that information I was able to take a look at some Swift calls I was already using from CorePlot and the associated Objective C code to figure out how to convert it.
This is what I came up with:
// original function
-(nullable CPTFill *)barFillForBarPlot:(nonnull CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
{
return CPTFill(color: .red()) // or whatever color you want based on the data value
}
// Swift version of the function (updated 2018/Oct(10)/07)
func barFill(for barPlot: CPTBarPlot, record index: UInt ) -> CPTFill?
{
return CPTFill(color: .red()) // or whatever color you want based on the data value
}
Thanks Eric.

Related

How do you select/highlight a part of the chart through code? (Charts- PieChartView)

I am using these classes to make charts:
https://github.com/danielgindi/Charts
.I currently have a pie chart displaying two options and I was wondering how you would highlight one of the options programmatically.
You can use the following methods to highlight part of the chart through code.
- (void)highlightValues:(NSArray<ChartHighlight *> * _Nullable)highs;
This should be used to programmatically highlight values. This DOES NOT generate a callback to the delegate.
- (void)highlightValue:(ChartHighlight * _Nullable)highlight;
highlight contains information about which entry should be highlighted.No call back to delegate.
- (void)highlightValueWithXIndex:(NSInteger)xIndex dataSetIndex:(NSInteger)dataSetIndex callDelegate:(BOOL)callDelegate;
/// Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index to undo all highlighting.
Examples :
1. [_chartView highlightValue:[[ChartHighlight alloc] initWithXIndex:0
dataSetIndex:0]];
2. [_chartView highlightValueWithXIndex:0 dataSetIndex:0
callDelegate:YES];
Swift 4
Charts (3.1.1)
You can choose an entry manually by highlightValue function.
If you want to highlight (select) by touch point, for example using UILongPressureGesture, it's possible to retrieve a potential highlight from chart and apply to its highlightValue function .
...
let point = gesture.location(in: chart)
let highlight = chart.getHighlightByTouchPoint(point)
chart.highlightValue(highlight)

Interactive 3d model for iPad

Similar to this question
Interactive 3D model without WebGL for iOS
I don't want to go down the webview/javascript path.
I want something like this
http://appserv.kfshrc.edu.sa/Default/Health/Anantomy.aspx
in native Metal or Scenekit written in Swift.
I've searched github but haven't come across anything.
Is this even possible? Reading through the SDKs, I can't see how to activate a surface, edge or vertex.
If you want to do this the easy way, I would suggest using SceneKit. You can make your each section of your human body model using any online 3D model maker, then export each section as a .dae, and import it into a SceneKit project. The default code in a SceneKit project already implements most of the features you would want, you would just have to modify which assets it displays (all of your files instead of ship.dae), change the positions of your models to line up correctly, and modify the handleTap method to display text in your HUD UI inside the if hitresults.count > 0 block, after the code that is already there. Basically you could modify the block of code to look something like:
if hitResults.count > 0 {
// retrieved the first clicked object
let result: AnyObject! = hitResults[0]
// get its material
let material = result.node!.geometry!.firstMaterial!
// highlight it
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)
// on completion - unhighlight
SCNTransaction.setCompletionBlock {
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)
material.emission.contents = UIColor.blackColor()
SCNTransaction.commit()
}
material.emission.contents = UIColor.redColor()
SCNTransaction.commit()
//this is where your custom code for handling a touch on an object goes. The variable "result" is the object that was tapped
self.myLabel.text = "Something Was Tapped"
}
So basically: 1. Make models 2. Put models in program (use the code the default project uses to load the ship) 3. Modify handleTap method to display information on the selected section. Hope it helps :)

Add annotation programmatically to core plot scatter plot give the X (date value) and Y

I am drawing a stock price chart using Core Plot objective-c library.
I need to add annotations to the graph (preferably custom view), programmatically, not on graph touch action. I have a list of x&y (x is a date and y is the price).
Is there an easy way to do this? and how?
Thank you.
Use this code block:
self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil];
// 8 - Add the annotation
[plot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation]; // change variable names accordingly
For more details, refer this one, answer for your question is already available here;
http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2
This tutorial is really helpful. Part-1 and Part-2 both.

JUNG Visualisation

Does anyone know if you are able to set the colour of the text for a vertex label in JUNG.
I'm using the Visualisation Viewer and can seem to be able to set the colour for everything else.
vv = new VisualizationViewer<String,Integer>(treeLayout, new Dimension(410,557));
Transformer<String,Paint> vertexPaint = new Transformer<String,Paint>() {
public Paint transform(String b) {
return Color.orange;
}
};
vv.setBackground(Color.white);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
//vv.getRenderContext().setVertexFontTransformer(vertexFont);
// add a listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.WHITE));
The DefaultVertexLabelRenderer and the DefaultEdgeLabelRenderer extend JLabel (it is similar to the way cell renderers work in JTable and JTree).
By default, it uses the foreground color of the VisualizationViewer to draw the label text.
vv.setForegroundColor(Color.red);
will make all of your labels red.
This approach is less expensive than making all of the labels parse HTML.
Sorry that the solution is so obscure.
Additionally, since the default renderers extend JLabel, the use of html is the same as it is for JLabel. There are good online resources to show examples of using html with javax.swing. What's missing is documentation to make the connection between using html in JUNG and using html in javax.swing.
You can use HTML in the label to specify the color; an example is here: https://stackoverflow.com/a/2017576/664856
In your case,
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.RED));
should work (if you wanted selected vertex to be Red). I tested it myself. This applies to the selected vertex.
Upon inspection of code, I would have to believe that the link I provided does correctly work for those vertices which are not selected, but I did not actually try implementing that link.

Can we Set the coreplot piechart slice color with image

I am using core plot in one of my ipad projects. Is it possible to set the color of all the slices in a pie chart with images.thanks in advance
Yes.
Implement the following method in your datasource and return a CPFill object that uses your image for the slice index requested.
-(CPFill *)sliceFillForPieChart:(CPPieChart *)pieChart recordIndex:(NSUInteger)index;

Resources