Cytoscape - headless mode example - cytoscape.js

I am unclear on how to get the headless mode (on node.js) to layout a graph and extract
the computed (per node) positioning information.
Below is the simplest example I could think of that should have worked, but doesn't.
What am I missing?
const cytoscape = require('cytoscape');
const fcose = require('cytoscape-fcose');
const elements = [
{ data: { id: 'n0' } },
{ data: { id: 'n1' } },
{ data: { id: 'n2' } },
{ data: { id: 'n3' } },
{ data: { id: 'e1', source: 'n0', target: 'n1' } },
{ data: { id: 'e2', source: 'n2', target: 'n3' } },
]
cytoscape.use(fcose); // register extension
const cy = cytoscape()
const layout = cy.layout({
name: 'fcose',
container: null,
layout: {
boundingBox: {
x1: 0,
y1: 0,
w: 600,
h: 600
},
},
elements,
headless: true,
styleEnabled: false,
animate: false,
ready: function () {
console.log(this)
}
}).run()
console.log(cy.nodes())

A working example:
const cytoscape = require('cytoscape');
const fcose = require('cytoscape-fcose');
const elements = [
{ data: { id: 'n0' } },
{ data: { id: 'n1' } },
{ data: { id: 'n2' } },
{ data: { id: 'n3' } },
{ data: { id: 'e1', source: 'n0', target: 'n1' } },
{ data: { id: 'e2', source: 'n2', target: 'n3' } },
]
cytoscape.use(fcose); // register extension
const cy = cytoscape({
container: null,
elements,
headless: true,
styleEnabled: false,
animate: null,
})
const layout = cy.layout({
name: 'fcose',
animate: null,
}).run()
cy.nodes().map((node, id) => {
console.log({
id,
position: node.position(),
boundingbox: node.boundingbox(),
})
})
Output:
{
id: 0,
position: { x: -10.739928259168892, y: 23.05192029205518 },
boundingbox: {
x1: -12.239928259168892,
y1: 21.55192029205518,
x2: -9.239928259168892,
y2: 24.55192029205518,
w: 3,
h: 3
}
}
{
id: 1,
position: { x: -34.115851865686764, y: -17.994146351728972 },
boundingbox: {
x1: -35.615851865686764,
y1: -19.494146351728972,
x2: -32.615851865686764,
y2: -16.494146351728972,
w: 3,
h: 3
}
}
{
id: 2,
position: { x: 34.115851865686764, y: -6.375158623550128 },
boundingbox: {
x1: 32.615851865686764,
y1: -7.875158623550128,
x2: 35.615851865686764,
y2: -4.875158623550128,
w: 3,
h: 3
}
}
{
id: 3,
position: { x: -4.785486637914051, y: -23.05192029205518 },
boundingbox: {
x1: -6.285486637914051,
y1: -24.55192029205518,
x2: -3.285486637914051,
y2: -21.55192029205518,
w: 3,
h: 3
}
}

Related

Nuxt.js show window is not defined when load to production

I have an application with nuxt.js as a framework and it's running well in development mode. However, when I deployed to the server for production, it's show "window is not defined".
I think it's because highcharts-vue.min.js module not loaded in components so shows this error but I'm not sure about that. I attach some code and screen shoot results after deploying on the server
<template>
<highcharts class="hc" :options="chartOptions" ref="chart">
</highcharts>
</template>
<script>
import {Chart} from 'highcharts-vue'
export default {
name:"ForecastCuaca",
components: {
highcharts: Chart
},
data() {
return {
data_cuaca:[],
chartOptions: {
chart: {
type: 'spline',
backgroundColor: 'transparent',
marginTop:100
},
title: {
text: 'Prakiraan Cuaca Kota Kupang',
style:{
"color": "#ebeefd",
"fontSize": "22px"
}
},
subtitle: {
text: 'Source: openweathermap.org',
style:{
"color": "#ebeefd",
}
},
xAxis: {
categories: ['Tanggal'],
labels:{
style:{
color:"#ebeefd"
}
}
},
yAxis: {
title: {
text: 'Suhu'
},
labels: {
formatter: function () {
return this.value + '°';
},
style:{
color:"#ebeefd"
}
},
style:{
"color": "#ebeefd",
}
},
tooltip: {
formatter: function () {
return 'Suhu : ' + this.y + '° C<br /><b>' + this.x + '</b>';
}
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1
}
}
},
series: [{
name: 'Kota Kupang',
marker: {
symbol: 'square'
},
data: [
10
],
style:{
"color": "#ebeefd",
}
}
],
credits:{
enabled:false
}
},
title: '',
}
},
methods:{
generate_data(datax){
const self = this;
self.data_cuaca = datax
self.chartOptions.xAxis.categories = []
self.chartOptions.series[0].data = []
let ganjil = 0;
for (let index = 0; index < self.data_cuaca.length; index++) {
ganjil++;
const element = self.data_cuaca[index];
self.chartOptions.xAxis.categories.push(element.waktu_indonesia)
if (ganjil % 2 == 1) {
const iconx = element.weather[0].icon+'.png';
const data_series = {
y:element.main.temp,
marker: {
symbol: "url(http://openweathermap.org/img/w/"+iconx+")"
}
}
self.chartOptions.series[0].data.push(data_series)
}else{
self.chartOptions.series[0].data.push(element.main.temp)
}
}
}
}
}
</script>

How can I call a method from options of ApexChart with vue.js

I'm new with vue and apex charts, basically what I need is to call a method from the apex chart options, I created a file showing the problem I'm having:
https://jsfiddle.net/wr3uo5va/
I need to call the method currencyValue from chartOptions.dataLabels
dataLabels: {
enabled: true,
offsetX: -25,
formatter: function(val) {
return val + " Reais"; <--- This works
// return this.currencyValue(val) <--- This does not work
},
},
Any suggestion ?
The problem is this inside the formatter callback is the chart instance (not the component instance) because it's declared as a regular function.
The solution is to use an arrow function to bind the component instance as the context:
export default {
methods: {
currencyValue(value) {⋯},
loadChartData() {
⋮
this.chartOptions = {
⋮
dataLabels: {
⋮
// ❌ don't use regular function here
//formatter: function(val) {
// return this.currencyValue(val)
//},
// ✅
formatter: (val) => {
return this.currencyValue(val)
},
},
}
}
}
}
updated fiddle
You can put chartOptions in methods instead of in data.
Below is working code
const currencyValue = (val) => {
return "R$" + val;
}
new Vue({
el: "#app",
data() {
return {
series: [450, 300, 500]
}
},
methods: {
chartOptions() {
return {
labels: ['Paid', 'Pending', 'Rejected'],
plotOptions: {
radialBar: {
size: 165,
offsetY: 30,
hollow: {
size: '20%'
},
track: {
background: "#ebebeb",
strokeWidth: '100%',
margin: 15,
},
dataLabels: {
show: true,
name: {
fontSize: '18px',
},
value: {
fontSize: '16px',
color: "#636a71",
offsetY: 11
},
total: {
show: true,
label: 'Total',
formatter: function() {
return 42459
}
}
}
},
},
responsive: [{
breakpoint: 576,
options: {
plotOptions: {
radialBar: {
size: 150,
hollow: {
size: '20%'
},
track: {
background: "#ebebeb",
strokeWidth: '100%',
margin: 15,
},
}
}
}
}],
colors: ['#7961F9', '#FF9F43', '#EA5455'],
fill: {
type: 'gradient',
gradient: {
// enabled: true,
shade: 'dark',
type: 'vertical',
shadeIntensity: 0.5,
gradientToColors: ['#9c8cfc', '#FFC085', '#f29292'],
inverseColors: false,
opacityFrom: 1,
opacityTo: 1,
stops: [0, 100]
},
},
stroke: {
lineCap: 'round'
},
chart: {
dropShadow: {
enabled: true,
blur: 3,
left: 1,
top: 1,
opacity: 0.1
},
},
tooltip: {
x: {
formatter: function (val) {
return val;
},
},
y: {
formatter: function (val) {
return currencyValue(val);
},
},
},
}
}
},
components: {
VueApexCharts
}
})
Methods can't be called in data or computed, they can be called in methods
One thing to be modified in html is below
<vue-apex-charts
type="donut"
:options="chartOptions()"
:series="series">
</vue-apex-charts>

Cytoscape js how to get all edges (text label) when clicked on a node

I know that I can use node.connectedEdges() to return an object of the edges connected to that node. How do I access the data fields of all the edges. I want to be able to click on a node and have the connected edges labels (text) in a list.
Also when you click on a node how can you access that nodes meta data (other attributes that are in the nodes JSON object).
Thanks
Well, everything in the node/edge metadata is accessible with the .data() method. This is a core functionality and i would suggest you to look at the docs, because this is a fairly simple task. All you have to do is to call the .connectedEdges() method and map the resulting object accordingly (using .data()):
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
style: [{
selector: 'node',
css: {
'content': 'data(id)',
'text-valign': 'center',
'text-halign': 'center',
'height': '60px',
'width': '60px',
'border-color': 'black',
'border-opacity': '1',
'border-width': '10px'
}
},
{
selector: 'edge',
css: {
'target-arrow-shape': 'triangle'
}
},
{
selector: ':selected',
css: {
'background-color': 'black',
'line-color': 'black',
'target-arrow-color': 'black',
'source-arrow-color': 'black'
}
}
],
elements: {
nodes: [{
data: {
id: 'n0'
}
},
{
data: {
id: 'n1'
}
},
{
data: {
id: 'n2'
}
},
{
data: {
id: 'n3'
}
},
{
data: {
id: 'n4'
}
},
{
data: {
id: 'n5'
}
},
{
data: {
id: 'n6'
}
},
{
data: {
id: 'n7'
}
},
{
data: {
id: 'n8'
}
},
{
data: {
id: 'n9'
}
},
{
data: {
id: 'n10'
}
},
{
data: {
id: 'n11'
}
},
{
data: {
id: 'n12'
}
},
{
data: {
id: 'n13'
}
},
{
data: {
id: 'n14'
}
},
{
data: {
id: 'n15'
}
},
{
data: {
id: 'n16'
}
}
],
edges: [{
data: {
source: 'n0',
target: 'n1'
}
},
{
data: {
source: 'n1',
target: 'n2'
}
},
{
data: {
source: 'n1',
target: 'n3'
}
},
{
data: {
source: 'n2',
target: 'n7'
}
},
{
data: {
source: 'n2',
target: 'n11'
}
},
{
data: {
source: 'n2',
target: 'n16'
}
},
{
data: {
source: 'n3',
target: 'n4'
}
},
{
data: {
source: 'n3',
target: 'n16'
}
},
{
data: {
source: 'n4',
target: 'n5'
}
},
{
data: {
source: 'n4',
target: 'n6'
}
},
{
data: {
source: 'n6',
target: 'n8'
}
},
{
data: {
source: 'n8',
target: 'n9'
}
},
{
data: {
source: 'n8',
target: 'n10'
}
},
{
data: {
source: 'n11',
target: 'n12'
}
},
{
data: {
source: 'n12',
target: 'n13'
}
},
{
data: {
source: 'n13',
target: 'n14'
}
},
{
data: {
source: 'n13',
target: 'n15'
}
},
]
},
layout: {
name: 'dagre',
padding: 5
}
});
cy.ready(function() {
cy.unbind('click')
cy.bind('click', 'node', function(event) {
let edges = event.target.connectedEdges().map(edge => edge.data().id)
console.log(edges)
})
})
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
float: left;
}
<html>
<head>
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
<script src="https://unpkg.com/dagre#0.7.4/dist/dagre.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cytoscape-dagre#2.1.0/cytoscape-dagre.min.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>
You just need some extra data in the edges and write that key instead of id:
edge.data().someKey

Edge Data Format

I am trying to find the most efficient way to return data for a network graph which will be constructed with cytoscape js on the front end. I know that the standard way to add edges and nodes is :
// can use reference to eles later
var eles = cy.add([
{ group: 'nodes', data: { id: 'n0' }, position: { x: 100, y: 100 } },
{ group: 'nodes', data: { id: 'n1' }, position: { x: 200, y: 200 } },
{ group: 'nodes', data: { id: 'n2' }, position: { x: 300, y: 300 } },
{ group: 'edges', data: { id: 'e0', source: 'n0', target: 'n1' } },
{ group: 'edges', data: { id: 'e0', source: 'n0', target: 'n2' } }
]);
Taken from their documentation
I was wondering if there is a way to effectively do the following:
// can use reference to eles later
var eles = cy.add([
{ group: 'nodes', data: { id: 'n0' }, position: { x: 100, y: 100 } },
{ group: 'nodes', data: { id: 'n1' }, position: { x: 200, y: 200 } },
{ group: 'nodes', data: { id: 'n2' }, position: { x: 300, y: 300 } },
{ group: 'edges', data: { id: 'e0', source: 'n0', target: ['n1', 'n2'] } }
]);
This way I can return an adjacency list from the server instead of an edge/node list.
Intresting format, but unfortunately, this won't work without maybe a new extension.
The thing is, that every element of the graph has to have an id! So when you add an edge to the graph and assign multiple targets, there ought to be 2 id's for every edge.
If you are trying to optimise your cy instance, you can go over this section of the documentation instead.

Update new Data to the Chart in 2.0Rc1

Here's the sample code
I tried with update and other functions, none of them worked
launch: function() {
var categories = ['EMEA','APAC','AMERICAS'],
name = 'Browser brands',
data = [{
y: 55.11,
drilldown: {
name: 'EMEA versions',
categories: ['Cost', 'TechMe', 'disc', 'Matt'],
data: [
{
y: 40.11,
drilldown: {
name: 'Cost Drill',
categories: ['Page 1', 'Page 2', 'Page 3.0', 'Page 4.0'],
data: [0.20, 0.83, 1.58, 13.12, 5.43]
}
},
{
y: 40.11,
drilldown: {
name: 'Techme Drill',
categories: ['Page 1', 'Page 2', 'Page 3.0', 'Page 4.0'],
data: [1.58, 13.12, 5.43,0.20, 0.83]
}
},
{
y: 40.11,
drilldown: {
name: 'dISC Drill',
categories: ['Page 1', 'Page 2', 'Page 3.0', 'Page 4.0'],
data: [0.83, 1.58, 13.12,0.20, 5.43]
}
}
]
}
}, {
y: 21.63,
drilldown: {
name: 'Firefox versions',
categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
data: [0.20, 0.83, 1.58, 13.12, 5.43]
}
}, {
y: 11.94,
drilldown: {
name: 'Chrome versions',
categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0',
'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'],
data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22]
}
}, {
y: 7.15,
drilldown: {
name: 'Safari versions',
categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon',
'Safari 3.1', 'Safari 4.1'],
data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14]
}
}, {
y: 2.14,
drilldown: {
name: 'Opera versions',
categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'],
data: [ 0.12, 0.37, 1.65]
}
}];
Here's the setChart function which is called on click event of the chart
function setChart(chart,name, categories, data, color) {
var newchart=mychart;
var chartConfig={
chart: {
type: 'column'
},
title: {
text: 'Browser market share, April, 2011'
},
subtitle: {
text: 'Click the columns to view versions. Click again to view brands.'
},
xAxis: {
categories: categories
},
yAxis: {
title: {
text: 'Total percent market share'
}
},
plotOptions: {
column: {
cursor: 'pointer',
point: {
events: {
click: function() {
var drilldown = this.drilldown;
if (drilldown) { // drill down
setChart(this,drilldown.name, drilldown.categories, drilldown.data, drilldown.color);
} else { // restore
setChart(this,name, categories, data);
}
}
}
},
dataLabels: {
enabled: true,
style: {
fontWeight: 'bold'
},
formatter: function() {
return this.y +'%';
}
}
}
},
tooltip: {
formatter: function() {
var point = this.point,
s = this.x +':<b>'+ this.y +'% market share</b><br/>';
if (point.drilldown) {
s += 'Click to view '+ point.category +' versions';
} else {
s += 'Click to return to browser brands';
}
return s;
}
}
}
console.log("Before ",newchart.chartConfig);
newchart.setChartConfig(chartConfig);
console.log("After ",newchart.chartConfig);
var chartSeries={
name: name,
data: data,
color: 'white'
}
newchart.setChartData(chartSeries);
chart.remove('#myChart');
//chart.update(newchart);
//redrawChart(this,mychart);
//console.log(mychart);
// console.log(chart.setChartConfig());
//chart.x.setCategories(categories, false);
//chart.chartData.series[0].remove(false);
/*chart.chartData.addSeries({
name: name,
data: data,
color: color || 'white'
}, false);*/
//chart.redraw();
// mychart.update();
//mychart.updateLayout();
// mychart.show();
console.log("After " , mychart);
}
Main chart is here ...
var mychart=Ext.create('Rally.ui.chart.Chart', {
autoRender:true,
autoShow:true,
id:'myChart',
storeType : 'Rally.data.WsapiDataStore',
storeConfig : {
model: 'Defect'
//fetch : ["_ValidFrom", "_ValidTo", "10778908645", "ScheduleState", "Name"]
//filters : this.getColumnFilters()
},
calculatorType: 'My.Calculator',
calculatorConfig: {},
chartConfig: {
chart: {
type: 'column'
},
title: {
text: 'Browser market share, April, 2011'
},
subtitle: {
text: 'Click the columns to view versions. Click again to view brands.'
},
xAxis: {
categories: categories
},
yAxis: {
title: {
text: 'Total percent market share'
}
},
plotOptions: {
column: {
cursor: 'pointer',
point: {
events: {
click: function() {
var drilldown = this.drilldown;
if (drilldown) { // drill down
setChart(this,drilldown.name, drilldown.categories, drilldown.data, drilldown.color);
} else { // restore
setChart(this,name, categories, data);
}
}
}
},
dataLabels: {
enabled: true,
style: {
fontWeight: 'bold'
},
formatter: function() {
return this.y +'%';
}
}
}
},
tooltip: {
formatter: function() {
var point = this.point,
s = this.x +':<b>'+ this.y +'% market share</b><br/>';
if (point.drilldown) {
s += 'Click to view '+ point.category +' versions';
} else {
s += 'Click to return to browser brands';
}
return s;
}
}
},
chartData:{ // this works
series: [{
name: name,
data: data,
color: 'white'
}],
exporting: {
enabled: false
}
}
});
// add your code here
this.add(mychart); // add the component to rally interface
}
I want data to get changed on clicking
I have an example of chart update triggered by a button click in a github repo here.
Unlike a AppSDK2 grids that have a reconfigure method, AppSDK charts do not. In this example I delete a chart this.down("#myChart").removeAll() , modify metric definition of the calculator and then re-add the chart to the app.
You may also want to see this post.