Hide / show charts in Lightning chart dashboard - lightningchart

I create 3 row chart like below
var charts = [];
const db = lightningChart().Dashboard({
numberOfRows: 3,
numberOfColumns: 1
})
charts[0] = db.createChartXY({
columnIndex: 0,
rowIndex: 0,
columnSpan: 1,
rowSpan: 1,
})
similiarly for charts[1] and charts[2]
Is there anyway we can hide charts[0] and show 1 and 2 ,, or show 2 and hide charts[0] and charts[1] programatically, like legends toggle ?

Currently only way to achieve this is to dispose the chart in the cell and then create a new chart on the same cell. This means that you would need to be able to create the charts from scratch every time you want to change which chart is visible.
To remove the existing chart you can call dispose() on the chart.
chart.dispose()
Then you can create the chart you want to show. You most likely want to make a function to create the chart for you.
const createChart1 = () => {
const chart = dashboard.createChartXY({
columnIndex: 0,
rowIndex: 0
})
chart.setTitle('Chart 1')
return chart
}
const {
lightningChart,
} = lcjs
const lc = lightningChart()
const dashboard = lc.Dashboard({
numberOfColumns: 1,
numberOfRows: 2
})
const createChart1 = () => {
const chart = dashboard.createChartXY({
columnIndex: 0,
rowIndex: 0
})
chart.setTitle('Chart 1')
return chart
}
const createChart2 = () => {
const chart = dashboard.createChartXY({
columnIndex: 0,
rowIndex: 0
})
chart.setTitle('Chart 2')
return chart
}
const createChart3 = () => {
const chart = dashboard.createChartXY({
columnIndex: 0,
rowIndex: 1
})
chart.setTitle('Chart 3')
return chart
}
// Initially have charts 1 and 3 on the dashboard
let c1 = createChart1()
const c3 = createChart3()
// After a timeout change the chart on the first row to chart 2
setTimeout(() => {
// Dispose to remove the chart 1
c1.dispose()
// Create the chart 2 on the place of chart 1
c1 = createChart2()
}, 1000)
<script src="https://unpkg.com/#arction/lcjs#2.2.1/dist/lcjs.iife.js"></script>

Related

Evenly distribute a given number of segments along an existing path

without seeing the codepen it is tricky to explain my situation, but here goes. I'm creating some paths by getting pathData using opentype.js. I am then placing random shapes at the position of the path's segment's points. Because of the nature of a font's paths some paths have far more segments than others for example '1' has way fewer segments thant '0'. I would like to average out the number of segments along each path so that when I add the shapes they look a consistent number of segments. Thanks in advance.
Is it possible to evenly distribute a given number of segments along an existing path?
Here is a link to the Codepen
paper.install(window);
const minMax = (min, max) => {
return Math.floor(Math.random() * max + min);
};
window.onload = () => {
paper.setup("canvas");
let pathData;
const font = opentype.load(
"https://assets.codepen.io/1070/pphatton-ultralight-webfont.woff",
(err, font) => {
if (err) {
console.log(err);
} else {
class Doughnut {
constructor(x, y) {
this.x = x;
this.y = y;
this.shape = new paper.Path.RegularPolygon({
position: [this.x, this.y],
sides: minMax(3, 8),
radius: minMax(6, 12),
fillColor: "black"
});
}
// makeShape(){
// this.shape
// }
}
pathData = font.getPath("100", 0, 600, 600).toSVG();
// const rect = new paper.Path.Rectangle({
// point: [80, 25],
// size: [300, 200],
// fillColor: "black"
// });
const number = new paper.Path(pathData);
number.selected = true;
// number.flatten(10);
const amount = 50;
const length = number.length
const points = [];
const segments = number.segments;
number.fitBounds(paper.view.bounds);
for(let i = 0; i < amount; i++){
const offset = i / amount * length
const point = number.getPointAt(offset)
new Doughnut(point.x, point.y);
}
segments.forEach((seg) => {
points.push(number.getPointAt(seg));
});
points.forEach((point) => {
console.log(point);
new Doughnut(point.x, point.y);
});
number.reduce();
}
}
);
const shapes = [];
class Doughnut {
constructor(x, y) {
this.x = x;
this.y = y;
this.shape = new paper.Path.RegularPolygon({
position: [this.x, this.y],
sides: minMax(3, 8),
radius: minMax(6, 12),
fillColor: "black"
});
}
// makeShape(){
// this.shape
// }
}
// for (let i = 0; i < 10; i++) {
// shapes.push(new Doughnut(minMax(100, 500), minMax(100, 500)));
// }
// console.log(shapes)
// shapes.makeShape()
};
The path.divideAt() method can help you greatly.
What is tricky in your case is that, in order to preserve the path appearance, you can't move the existing segments. So you'll have to find a way to only add segment where it is needed.
Otherwise, here's a simple sketch demonstrating a possible solution. It should get you on the track to find a solution more specific to your use case.
const circle = new Path.Circle({
center: [0, 0],
radius: 75,
selected: true
});
const rectangle = new Path.Rectangle({
from: [0, 0],
to: [200, 100],
selected: true
});
rectangle.position = circle.position + [circle.bounds.width + rectangle.bounds.width, 0];
const cloneAndAddSegments = (item) => {
const clone = item.clone().translate(0, 200);
const length = clone.length;
const step = 20;
const iterations = Math.floor(length / step);
for (let i = 1; i <= iterations; i++) {
const offset = i * step;
clone.divideAt(offset);
}
return clone;
};
const circleClone = cloneAndAddSegments(circle);
const rectangleClone = cloneAndAddSegments(rectangle);
const showSegments = (item) => {
item.segments.forEach(({ point }) => new Path.Circle({
center: point,
radius: 5,
fillColor: 'orange'
}))
}
showSegments(circle);
showSegments(rectangle);
showSegments(circleClone);
showSegments(rectangleClone);
project.activeLayer.fitBounds(view.bounds.scale(0.8));

In LightningChartJS, is there a way to do a drag zoom where the zoom for X and Y is always equal?

My problem is when I drag rectangle zoom for example (gyazo gif), the X axis is wider than the Y. In effect, the X axis is zoomed in more details than the Y and the visual of the graph looks different from the original graph.
https://gyazo.com/749db917465a8037b7c5f21792f572ce
I am looking for a way where if i zoom rectangle drag, the function is similar to zooming via mousewheel where x and y zoom feels equal.
here you will find an example how you can perform custom zoom rect instead of default and keep the ratio of the axes.
// Extract required parts from LightningChartJS.
const {
ColorRGBA,
ColorHEX,
emptyFill,
SolidFill,
SolidLine,
translatePoint,
_point,
lightningChart
} = lcjs;
// Import data-generator from 'xydata'-library.
const {
createProgressiveTraceGenerator
} = xydata
const chart = lightningChart()
.ChartXY()
// Disable default chart interactions with left mouse button.
// .setMouseInteractionRectangleFit(false)
.setMouseInteractionRectangleZoom(false)
.setTitleFillStyle(emptyFill)
// generate data and creating the series
const series = chart.addLineSeries().setStrokeStyle(
new SolidLine({
fillStyle: new SolidFill({ color: ColorHEX('#fff') }),
thickness: 2,
}),
)
// generate data and create series
createProgressiveTraceGenerator()
.setNumberOfPoints(200)
.generate()
.toPromise()
.then((data) => {
return series.add(data)
})
// create zooming rect and dispose it
const rect = chart
.addRectangleSeries()
.add({
x: 0,
y: 0,
width: 0,
height: 0,
})
.setFillStyle(new SolidFill({ color: ColorRGBA(255, 255, 255, 30) }))
.setStrokeStyle(
new SolidLine({
thickness: 2,
fillStyle: new SolidFill({ color: ColorRGBA(255, 255, 255, 255) }),
}),
)
.dispose()
// om Mouse drag restor rectange and set position and coordinates
chart.onSeriesBackgroundMouseDrag((obj, event, button, startLocation, delta) => {
if (button !== 0) return
const startLocationOnScale = translatePoint(
chart.engine.clientLocation2Engine(startLocation.x, startLocation.y),
chart.engine.scale,
series.scale,
)
const curLocationOnScale = translatePoint(chart.engine.clientLocation2Engine(event.x, event.y), chart.engine.scale, series.scale)
const x = Math.abs(series.getBoundaries().min.x) + Math.abs(series.getBoundaries().max.x)
const y = Math.abs(series.getBoundaries().min.y) + Math.abs(series.getBoundaries().max.y)
const ratio = x / y
const width = Math.abs(curLocationOnScale.x - startLocationOnScale.x)
const height = Math.abs(curLocationOnScale.y - startLocationOnScale.y)
const heightDirection = curLocationOnScale.y - startLocationOnScale.y // check direction of rect
// check for mouse direction to prevet fit and zoom conflict
if (curLocationOnScale.x > startLocationOnScale.x) {
rect.setDimensions({
x: startLocationOnScale.x,
y: startLocationOnScale.y,
width: width > height * ratio ? width : height * ratio,
height: width > height * ratio ? (heightDirection > 0 ? width : -width) / ratio : heightDirection,
}).restore()
} else {
// prevent phantom rectangle if you change zoom to fit during the dragging
rect.setDimensions({
x: 0,
y: 0,
width: 0,
height: 0,
}).dispose()
}
})
// on mouse drag stop dispose rect and zoom relative to its dimensions
chart.onSeriesBackgroundMouseDragStop((_, event, button, startLocation) => {
if (button !== 0) return
const rectZooom = rect.getDimensionsPositionAndSize()
if (rectZooom.width !== 0) {
chart.getDefaultAxisX().setInterval(rectZooom.x, (rectZooom.x + rectZooom.width), true, true)
if(rectZooom.height > 0){
chart.getDefaultAxisY().setInterval(rectZooom.y, (rectZooom.y + Math.abs(rectZooom.height)), true, true)
}else{
chart.getDefaultAxisY().setInterval((rectZooom.y - Math.abs(rectZooom.height)),rectZooom.y, true, true)
}
}
rect.setDimensions({
x: 0,
y: 0,
width: 0,
height: 0,
}).dispose()
})
<script src="https://unpkg.com/#arction/xydata#1.4.0/dist/xydata.iife.js"></script>
<script src="https://unpkg.com/#arction/lcjs#3.0.0/dist/lcjs.iife.js"></script>

zoomband is displayed as lineseries when i add a stepseries to a chartXY?

i find that zoombandchart is displayed as lineseries when i add a stepseries to a chartXY,
what i expected is that the series in zoombandchart as stepseries, which is same as in chart,
so how to fixed this problem? here is my code:
const {
lightningChart,
StepOptions,
UILayoutBuilders,
UIElementBuilders,
UIButtonPictures,
UIBackgrounds,
UIOrigins,
emptyFill,
emptyLine,
AxisTickStrategies,
SolidFill,
Themes,
createProgressiveTraceGenerator
} = lcjs
const dashboard = lightningChart().Dashboard( {
numberOfColumns: 1,
numberOfRows: 4
} )
const chart = dashboard.createChartXY( {
columnIndex: 0,
columnSpan: 1,
rowIndex: 0,
rowSpan: 3
})
const zoomBandChart = dashboard.createZoomBandChart({
columnIndex: 0,
columnSpan: 1,
rowIndex: 3,
rowSpan: 1,
axis: chart.getDefaultAxisX()
})
zoomBandChart
.setTitleFillStyle(emptyFill)
zoomBandChart.getDefaultAxisX()
.setTickStrategy( AxisTickStrategies.Empty )
zoomBandChart.getDefaultAxisY()
.setAnimationScroll(false)
const line = chart.addStepSeries({mode:StepOptions.after})
line.add([
{x:0,y:1},
{x:1,y:0},
{x:2,y:1},
{x:3,y:1},
{x:4,y:1},
{x:5,y:1},
{x:6,y:1},
])
<script src="https://unpkg.com/#arction/lcjs#2.1.0/dist/lcjs.iife.js"></script>

Is there a better way of writing this data structure in Dart?

Map in Dart is an hash table, am I right?
Map starsData = {
'stars':{
'star1': {'x': 0, 'y': 10},
'star2': {'x': 0, 'y': 10}
}
};
This object below in JavaScript can be accessed as an hash table, faster!! I just want to do the some in Dart, but I am not sure if the best way is using Map.
const starsData = {
stars:{
'star1': {'x': 0, 'y': 10},
'star2': {'x': 0, 'y': 10}
}
};
I have rewritten your JavaScript implementation (based on the project your linked: https://github.com/ToniCalfim/fallingstars/blob/master/index.js) in Dart:
Can also be tested with:
https://dartpad.dev/900989f4e35e5a61200e4ad04ecd399a
import 'dart:html';
import 'dart:math' as math;
const starsDiameter = 1.25;
const colorPallete = [
'white',
'yellow',
'blue',
'red',
'orange',
'turquoise',
'purple',
'green',
'lightblue',
'lightyellow',
'lightgreen',
'darkred',
'darkblue',
'darkorange',
'darkturquoise',
'darkgreen'
];
final math.Random _rnd = math.Random();
int getRandomNumber(int min, int max) => min + _rnd.nextInt(max - min);
class Star {
int x, y;
int positionX = getRandomNumber(2, 650);
int positionY = getRandomNumber(3, 125);
double diameter = starsDiameter;
int pulsing = 0;
int blinking = 0;
int timeToFall = getRandomNumber(0, 7500);
int velocityToFall = getRandomNumber(1, 5);
int directionToFall = getRandomNumber(-1, 1);
String color = colorPallete[getRandomNumber(0, colorPallete.length)];
Star() {
x = positionX;
y = positionY;
}
}
final List<Star> stars = List.generate(175, (_) => Star());
void update() {
for (final currentStar in stars) {
final currentTimeToFall = currentStar.timeToFall;
if (currentTimeToFall != 0) {
currentStar.timeToFall = currentTimeToFall - 1;
} else {
final currentVelocityToFall = currentStar.velocityToFall;
final currentAngleToFall = currentStar.directionToFall;
final currentPositionX = currentStar.x;
final currentPositionY = currentStar.y;
currentStar.x = currentPositionX + 1 * currentAngleToFall;
currentStar.y = currentPositionY + currentVelocityToFall;
}
}
}
final CanvasElement canvas = querySelector('#canvas') as CanvasElement;
final CanvasRenderingContext2D context2D = canvas.context2D;
void drawStars() {
context2D.clearRect(
0, 0, context2D.canvas.width, context2D.canvas.height); // Clear canvas
for (final currentStar in stars) {
context2D.beginPath();
context2D.fillStyle = currentStar.color;
context2D.arc(currentStar.x, currentStar.y, starsDiameter, 0, 2 * math.pi);
context2D.fill();
context2D.closePath();
}
}
void animateLoop([num highResTime]) {
update();
drawStars();
window.requestAnimationFrame(animateLoop);
}
void main() {
animateLoop();
}
By looking at your code I could not see any reason why the stars should be saved in a Map or other Hash tables related structure. You are using the stars in two ways: draw and update. In both cases your are just going through all the stars which can be done by using a simple list and iterate over all elements.
I should add that I am not a front-end programmer and I cannot really judge if the way your are drawing the 2D canvas is the most efficient way to do that. My converted code are only are attempt to show how the data could be structured in Dart.

How to add additionalIcons to the tile in a Band

I am developing Microsoft Band2 Apps, I want to add Additional icons to my app tile in a band, like setting tile in Band2. I followed the below code to add Additional icons to the tile
Guid pageguid = Guid.NewGuid();
var panel = new ScrollFlowPanel
{
Rect = new PageRect(0, 0, 260, 128),
};
panel.Elements.Add(new FlowPanel
{
Rect = new PageRect(0, 0, 32, 32),
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center,
});
panel.Elements.Add(new Icon
{
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center,
Rect= new PageRect(0, 0, 32, 32),
ColorSource= ElementColorSource.BandHighlight,
});
var layout = new PageLayout(panel);
BandTile tile = new BandTile(tileGuid)
{
Name = "EmergencyHelp ",
TileIcon = await LoadIcon("ms-appx:///Assets/User that is member of security group.png"),
SmallIcon = await LoadIcon("ms-appx:///Assets/User that is member of security groupSmall.png"),
};
int firstIconIndex = tile.AdditionalIcons.Count + 2; // First 2 are used by the Tile itself
tile.AdditionalIcons.Add(await LoadIconMethod(AdjustUriMethod("ms-appx:///Assets/User that is member of security groupSmall.png")));
pageLayoutData.ById<IconData>(3).IconIndex = (ushort)(firstIconIndex + 0);
tile.PageLayouts.Add(layout);
if (tile.TileId != null)
{
await client.TileManager.RemoveTileAsync(tile.TileId);
}
await client.TileManager.AddTileAsync(tile);
await client.TileManager.SetPagesAsync(
tileGuid,
new PageData(pageguid, 0, new TextButtonData(1, "Emergency")));
I am getting the exception as shown in below figure,

Resources