How to plot multiple lines with shared x-axis in react? - reactjs

Its just basic it the figure that I want. All graphs have a same x axis but different y axis.
I found some type of this in subplots of plotly but they were not exactly as I wanted.
I found another question with answer:
Plotly: How to plot multiple lines with shared x-axis?
But I need it in react.
Thanks!

Use library. One of a powerful library for plotting charts is echarts. Have following link for reviewing examples:
https://echarts.apache.org/examples/en/

I think this Library has the feature you are looking. But please see license before using :-)
https://js.devexpress.com/Demos/WidgetsGallery/Demo/Charts/Overview/jQuery/Light/

I found a simple react library called Rechart.
The graphs is called Synchronized Line chart. You can meddle around with the UI to get your desired result.
http://recharts.org/en-US/examples/SynchronizedLineChart

This is a good question - it's not readily obvious how to do this with Plotly in React. Although Plotly's docs explain the syntax well for vanillaJS it does require some adapting. I was able to get this working, and following along with this video on YouTube was helpful.
In short, since the <Plot /> component for React accepts a property called 'data' which expects an array, you need to call a function here that returns an array of Plotly Trace objects. The component might look something like this, depending on your options etc:
<Plot
data={createLines()}
layout={{ width: 960, height: 600 //...etc }}
/>
Note: My data formatting may be a bit different than others...I have a multiple lines to plot on the Y axis, with the x-axis all sharing an array of timesteps? Either way, you'll want to pass in the same array of data for your x axis to each line chart trace. I'm handing my component a prop called 'lineData' which looks like this, where each key is the variable name the user has selected, and the value is the array of data points to use on the y Axis:
//props.lineData would log like below, where each key becomes the name of a line
[
{"lineOne": [1, 3, 14, 37 ...]},
{"lineTwo": [14, 87, 12, 9 ...]}
]
Here is my createLines function below...note that I'm using props.timeData on the x axis for each trace:
const createLines = () => {
let traces = [];
props.lineData.map(line => {
Object.entries(line).forEach(([key, value])=>{
traces.push({
name: key,
x: props.timeData,
y: value,
type: 'scatter',
mode: 'lines'
})
})
})
return traces
}

Related

What is the difference in layout positioning of react-vr and react-360?

I had started creating a project with react-vr and had laid out the object in a spherical manner around the user.
Or for example, the 3 objects had positions (1, 1, 1), (2, 2, 2) and (3, 3, 3).
style: { transform: [{ translate: [1,1,1] }] }
On moving to react-360 the same positions of the objects seem quite different. From the initial view, all three object appear to be in a straight vertical column.
Is there some major difference in the layout structure of the two that I've missed here?
Also the order of rendering of these objects differ the output that we are getting in react-360.
There is a difference that by default it's rendered to 2D Surface, not 3D scene. I must say for me it's quite strange and i also struggled with this. Especially since they changed it significantly and is hard to find any info about it in docs (if it even is there because I found a solution in code). So, in client.js file you have to change this
r360.renderToSurface(
r360.createRoot('YourProject', { /* initial props */ }),
r360.getDefaultSurface()
);
to this
r360.renderToLocation(
r360.createRoot('YourProject', { /* initial props */ }),
r360.getDefaultLocation()
);
Then you will be able to position objects like in React VR.

Angular-NVD3 Stacked Bar Chart With Line

I'm new to D3, NVD3 and Angular-NVD3. I need to create a chart that looks similar to this, using Angular-NVD3:
SO question: NVD3 Stacked Bar Chart Plus Line Overlapped
(there's a JS Fiddle on the SO question).
Angular-NVD3 has two charts which each accomplish most of what I need, but not all of it:
Those would be the MultiBar and the MultiChart. (http://krispo.github.io/angular-nvd3/#/quickstart)
Examples:
That is, I need a stacked bar chart with a line (or lines) overlaid on top of the bar chart. I believe that to do this, one must use a "multiChart" instead of "multiBar" chart. (Please correct me if I'm wrong).
The SO example above modifies the NVD3 library, which I would like to avoid. After researching, I can see why they chose this approach.
nv.d3.js (version 1.8.2), for the multiChart object, Lines 9068-9069 :
bars1 = nv.models.multiBar().stacked(false).yScale(yScale1),
bars2 = nv.models.multiBar().stacked(false).yScale(yScale2),
If you change these lines to .stacked(true), it does stack the bars.
How can I use Angular-NVD3 and accomplish what I need without changing the library?
Ideally the solution would be pure configuration. The next most ideal situation would be to set the "stacked" value at runtime from inside my enclosing component, but I can't seem to get access to the object.
I would like to avoid a highly intricate solution if possible because the whole point of these abstractions is to avoid such complexities. However, all solutions are welcome.
I found the answer:
you can change the stacked value in the callback() event handler, specified in your config:
chart: {
type: 'multiChart',
...
callback: function (chart) {
chart.bars1.stacked(true);
chart.bars2.stacked(true);
chart.update();
},
...
}
};
Use multiChart. To get the bars side by side set yAxis: 1 on both bar data sets.
https://github.com/novus/nvd3/blob/master/examples/multiChart.html
(the repo examples cover more then the website)

Formatting Chartist Labels using Angular-Chartist and CoffeeScript

I'm trying to format my labels as percentages. I've tried looking at the documentation for Chartist.js and converting it to CoffeeScript, the problem is things aren't quite as clear to me since we're using Angular and therefore the angular-chartist module. It's a fairly trivial piece that I can't ask my co-founder to spend time on because there are many larger pieces at play in our project, but I would like to understand where I'm coming up short.
The chart is displayed using a chartist directive (which I'm guessing is a part of angular-chartist):
<chartist class="ct-chart" chartist-data="typeCounts" chartist-chart-type="Bar" chartist-responsive-options="typeCounts.barResponsiveOptions"></chartist>
This is my coffeescript for trying to get the options in (note that the labels and series properties are working fine; but the chartist element is not picking up the barResponsiveOptions property (therefore the console.log debug line is not firing):
# Organize return data into labels and series for Chartist
typeCounts = ResultService.getTypeCounts()
$scope.typeCounts.labels = Object.keys(typeCounts)
$scope.typeCounts.series = [typeCounts[type] for type in Object.keys(typeCounts)]
$scope.typeCounts.barResponsiveOptions = [
axisY:
labelInterpolationFnc: (value) ->
console.log("Firing barResponsiveOptions")
Math.round(value * 100) + '%'
]
Right now the chart displays with the data points on the y-axis as fractions of 1 (e.g. 0.0 -> 1.0).
You should use the chartist-chart-options attribute for your regular options and chartist-responsive-options if you're not using responsive options as explained here https://gionkunz.github.io/chartist-js/getting-started.html#responsive-sugar-topping.
Cheers

Set Parent of Map Axes in Matlab GUI

I am programming a basic GUI in MATLAB that utilizes the mapping toolbox. The GUI will display a grayscale image and then plot discrete points over the data, all of this over the necessary map projection. It is important that I plot onto map axes (those created by the axesm command) rather than the vanilla cartesian space. I have no problem doing all this from the command line, but I cannot find a way to implement a GUI version and its driving me nuts.
The problem is that I need to specify the map axes as being the child of the parent figure. The normal axes has a property that can be set, doing something like:
axesHandle = axes('Parent', parentHandle, ...);
or
set(axesHandle, 'Parent', parentHandle);
However, there is no equivalent parent property for the map axes created by the axesm function, so I have no way to manipulate the axes within the figure. How can I do this?
Update: If I create a plot within the map axes in an empty figure, get(figureHandle, 'Children') returns the handle of the axesm object (thanks #slayton!), so the map axes object must be implicitly added to the children of the figure by MATLAB.
Should I be concerned that the map axes do not refer back to the parent figure, or should I just let it be? I wonder if this is a classic case of MATLAB forcing me to not comply with the standards the manual tells me to implement.
From reading your question what I think you are trying to do is grab the handle of the axes object. This can be done as the axes is created using either axes or subplot
a = axes();
a = subplot(x,y,z);
% both return an handle to the newly created axes object
Additionally if the axes is created automagically by a function call like plot or image you can get the axes handle that too:
p = plot(1:10); %returns a handle to a line object
a = get(p,'Parent');
i = image(); %returns a handle to an image object
a = get(i, 'Parent');
Finally, neither of those two options is available you can always get the axes handle from its containing figure with:
a = get(figureHandle, 'Children');
Remember though that this will return a vector of axes handles if your figure contains more than one axes.
Finally when it comes time to draw draw your points to the axes that contains your map image you simply need to call:
line(xPoints, yPoints, 'linestyle', 'none', 'marker', '.', 'color', 'r', 'size', 15)
This will draw the vertices of the line using large red dots.
I'm not sure if this answers your question because the code you provided doesn't line up with the question you asked.
The code you provided looks like you are trying to move an axes from one figure to another. You can totally do this!
f = figure('Position', [100 100 100 100]);
a = axes('Parent', f);
pause
f2 = figure('Position', [250 100 100 100]);
set(a,'Parent', f2);
After much trial and error and reading of documentation, I have found that there is no way to explicitly specify the parent of the map axes. Instead, they are implicitly added on top of the current axes. In the instance that no axes exist in the current figure, calling axesm creates an axes object and then places the axesm object inside. When you take this route, you have to grab the axes object handle by calling gca:
mapAxesHandle = axesm(...);
axesHandle = gca(...);
This makes it frustrating to use the mapping toolbox when writing a GUI from scratch, but that's the way Mathworks makes it happen. Thanks to #slayton for useful info. I'd upvote but my reputation is <15 :(

Changing one single marker in a series in ExtJS charts

this is what I'm trying to do...
I have a chart with a line series with markers in it. In the X axis I have dates and in the Y axis a number. So lets say I have in x dates from yesterday to next week. And each day has a corresponding Y axis value.
What I want to do is change the color of the marker that belongs to the actual date.
In other words, to make it clearer, I want to change the color of a single marker in a ExtJS line series. I know of markerConfig but that doesn't seem to help since it applies to all markers.
I haven't found anything like this around so I stopped here to see if you guys could help me.
Thanks in advance!
I think the easiest way to do this is to highlight a single datapoint in a series.
First, define your highlight style.
Ext.create('Ext.chart.Chart', {
...
series: [{
type: 'line',
highlight: {
size: 10,
radius: 10,
fill: 'red'
},
...
});
Then, select the appropriate datapoint in your series and call the highlightItem() method. Here's an example showing how to use the method.
var series = myChart.series.get(0);
series.highlightItem(series.items[2]); // highlight the 3rd data point in the series
The result would look something like this.

Resources