Plotly chart area cuts off text - charts

I have the following plotly code:
var element = document.getElementById(scope.changeid);
function getData(division,redraw) {
var employeeData = [];
if (!division) {
$http.get(api.getUrl('competenceUserAverageByMyDivisions', null)).success(function (response) {
processData(response,redraw);
});
}
else {
$http.get(api.getUrl('competenceUserAverageByDivision', division)).success(function (response) {
processData(response,redraw);
})
}
}
function processData(data,redraw) {
var y = [],
x1 = [],
x2 = [];
data.forEach(function (item) {
y.push(item.user.profile.firstname);
x1.push(item.current_level);
x2.push(item.expected);
});
var charData = [{
x: y,
y: x1,
type: 'bar',
name: $filter('translate')('COMPETENCES.GRAPH.CURRENT'),
marker: {
color: '#23b7e5'
}
}, {
x:y,
y:x2,
type: 'bar',
marker: {
color: '#f05050'
},
name: $filter('translate')('COMPETENCES.GRAPH.EXPECTED')
}],
layout = {
title: $filter('translate')('USERMANAGEMENT.USERS'),
barmode: 'stack',
legend: {
traceorder: 'reversed'
}
};
Plotly.newPlot(element,charData,layout);
}
scope.$watch('divisionId', function (newValue, oldValue) {
if (newValue) {
getData(newValue.id,true);
}
}, true);
getData(null,false);
This generates the following chart:
<div class="panel-body">
<h4 class="text-center">{{'COMPETENCES.GRAPH_TITLES.OVERVIEW_GAP' | translate}}</h4>
<vertical-bar-chart id="chartArea" goto="competence.titleCompetenceDetails"
changeid="chartArea" xcolumn="xColumn" y-column="yColumn"
dataset="dataSet"
has-addition="true"
style="width: 80%; text-align: center"></vertical-bar-chart>
</div>
As you might be able to tell the text (x column) is being unintentionally cut off. So my question is how can i avoid this? i have attempted to increase the height of the element however without any luck :(
AS you can see here:
(oh you cant tell because of the white background but the height of panel body is 1000 px however it still cuts it off.)

Try increasing the bottom margin in layout.margin.b (more info in the plotlyjs reference page.

For reference, I had the same issue, margin bottom didn't help, but after the graph was created, I ran the following JQuery which revealed the hidden text:
var heightincrease = $('#yourID').find('.svg-container').height() + 100;
$('#yourID').find('.svg-container').height(heightincrease).find('.main-svg').height(heightincrease);
Obviously adjust as required to reveal your whole graph. Will probably break resizing so will need work if that's a concern.

Related

Adding inner padding to chart [chart.js v3]

I'm trying to add extra padding to my chart (extra space at the right of the "orange" column):
But using chart 3.7.0 doesn't seem to work, any idea of how I can achieve this??
chart.options.scales:
scales: {
x: {
afterFit(axis) {
axis.paddingLeft = 50; //doesn't work
axis.paddingRight = 50; //doesn't work
},
},
},
Full Runnable example.
Chart.js docs state following
let chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
layout: {
padding: {
right: 50
}
}
}
});

GoldenLayout hide/show component (again)

I have an issue with showing/hiding a component similar to this question:
GoldenLayout, how to hide/show component?
My layout is as follows:
let config: Config = {
settings: {
showCloseIcon: false,
showPopoutIcon: false
},
content: [
{
type: 'column',
content: [
{
type: 'row',
height: 25,
content: [
{
title: 'A react component',
type: 'react-component',
component: 'searchContainer'
}
],
},
{
type: 'row',
height: 75,
content: [
{
title: 'A react component',
type: 'react-component',
component: 'leftContainer'
},
{
title: 'Another react component',
type: 'react-component',
component: 'rightContainer'
}
],
},
],
}],
};
I have a hideSearchBar and showSearchBar functions which look like this:
function hideSearchBar() {
let container: ContentItem = layout.root.contentItems[0];
container.contentItems[1].config.height = 100;
container.contentItems[1].contentItems[0].config.height = 100;
container.contentItems[1].contentItems[1].config.height = 100;
container.config.height = 0;
container.contentItems[0].element.hide();
layout.updateSize();
//layout.updateSize($(window).width(), $(window).height());
}
function showSearchBar() {
let container: ContentItem = layout.root.contentItems[0];
container.contentItems[0].element.show();
layout.updateSize();
}
The showSearchBar works perfectly and shows both rows of the grid correctly.
The hideSearchBar hides the top row correctly but leaves the second row does not take up the whole screen. I have tried setting the config.height to 100 in various places but cannot get it to work - there is a gap the size of the top row at the bottom of the screen.
Any help much appreciated.
I solved this with a different layout config where search bar was initially set to 0:
let config: Config = {
settings: {
showCloseIcon: false,
showPopoutIcon: false
},
content: [
{
type: 'column',
content: [
{
type: 'row',
height: 0,
content: [
{
title: 'A react component',
type: 'react-component',
component: LayoutComponent.SearchContainer
}
],
},
{
type: 'row',
height: 100,
content: [
{
title: 'A react component',
type: 'react-component',
component: LayoutComponent.WindowContainer
},
{
title: 'Another react component',
type: 'react-component',
component: LayoutComponent.CollectionContainer
}
],
},
],
}],
};
showSearchBar looks like this:
function showSearchBar() {
let container: ContentItem = layout.root.contentItems[0];
if (searchRowHeight == 0) {
container.contentItems[0].config.height = SEARCH_HEIGHT;
}
else {
container.contentItems[0].config.height = searchRowHeight;
container.contentItems[1].config.height = containerRowHeight;
}
container.contentItems[0].element.show();
layout.updateSize();
}
and hideSearchBar looks like this:
function hideSearchBar() {
let container: ContentItem = layout.root.contentItems[0];
container.contentItems[0].config.height = 0;
container.contentItems[1].config.height = 100;
container.contentItems[0].element.hide();
layout.updateSize();
}
In summary, the config made the searchBar hidden and when it was opened, heights were readjusted.
I use an event listener to check for height changes:
layout.on('stateChanged', () => {
let updateConfig: Config = layout.toConfig();
if (updateConfig.content[0].content[0].height != 0) {
searchRowHeight = updateConfig.content[0].content[0].height;
containerRowHeight = updateConfig.content[0].content[1].height;
}
HTH
Extending #jmc42's answer. Pretty good work-around but once thing it doesn't do is hide the splitter when expanding on pane to 100% and the collapsing the other to 0%.
As a work-around, I thought of 2 choices:
When the pane gets hidden, get the adjacent element representing the splitter bar within the same div and hide it.
When the pane gets hidden, and you detect a resize, always re-apply the expand the top pane to 100% and the bottom pane to 0%.
I opted for option 2 as it was simpler to implement and what I have is:
if (updateConfig.content[0].content[0].height != 0) {
searchRowHeight = updateConfig.content[0].content[0].height;
containerRowHeight = updateConfig.content[0].content[1].height;
}
else {
let container = gbl_Layout.root.contentItems[0].contentItems[0];
container.contentItems[0].config.height = 100;
container.contentItems[1].config.height = 0;
layout.updateSize();
}
My 'if' statement condition is more complex that the one above as I'm performing other checks but that will give you the gist of it. Works pretty well for me.

How do I leave the clicked point highlighted in dygraphs?

I am using the selected shapes to draw a larger diamond shape on my graph. When a user clicks a point. I display the data in another div, but I want to leave the clicked point highlighted. In other words, I want to 'toggle' data behind the points on and off and the clicked points need to show if they are included in the dataset. I believe I have seen this somewhere but I cannot find it. Is there a 'standard' way of leaving a clicked point in the 'highlight' state when you mouse away after clicking?
Here is my code. The pointClickCallback is getting the data through ajax and displaying it in another div. That part works. I just want to leave the point highlighted so I know which points I have clicked on.
I also need the point to revert back to normal when I click a second time. This is a toggle that allows me to select and unselect points.
EDIT: I found the interaction model example but when I add it to my code I lose my pointClickCallback functionality. I saw the call to captureCanvas and the interaction model structure.
var g = new Dygraph(document.getElementById('rothmangraph'), lines, {
//showRangeSelector: true,
title: "Personal Wellness Index (PWI)",
labels: ['Date', 'Index'],
color: ['#006699'],
valueRange: [0, 101],
axisLabelFontSize: 12,
drawPoints: true,
gridLineColor: "#aaaaaa",
includeZero: true,
strokeWidth: 2,
rightGap: 20,
pointSize: 4,
highlightCircleSize: 8,
series : {
Index: {
drawHighlightPointCallback : Dygraph.Circles.DIAMOND
},
},
axes: {
y: {
pixelsPerLabel: 20,
},
x: {
valueFormatter: function(ms) {
return ' ' + strftime('%m/%d/%Y %r',new Date(ms)) + ' ';
},
axisLabelWidth: 60,
axisLabelFormatter: function(d, gran) {
return strftime('%m/%d %I:%M %p',new Date(d.getTime())) ;
}
}
},
underlayCallback: function (canvas, area, g) {
var warning = g.toDomCoords(0,41);
var critical = g.toDomCoords(0,66);
// set background color
canvas.fillStyle = graphCol;
canvas.fillRect(area.x, area.y, area.w, area.h);
// critical threshold line
canvas.fillStyle = "#cc0000";
canvas.fillRect(area.x,warning[1],area.w,2);
// warning threshold line
canvas.fillStyle = "#cccc00";
canvas.fillRect(area.x,critical[1],area.w,2);
},
pointClickCallback: function(e,point) {
var idx = point.idx;
var line = lines[idx];
var sqltime = strftime('%Y-%m-%d %H:%M:%S',new Date(line[0]));
var dispdate = strftime('%m/%d %r',new Date(line[0]));
_secureAjax({
url: '/ajax/getDataPoint',
data: {'patient_id': pid, "rdate": sqltime},
success: function (result) {
// parse and add row to table if not exists.
var data = JSON.parse(result);
var aid = data['id'];
var indexCol = "#a9cced"
if (line[1] <= 65) indexCol = "#ede1b7";
if (line[1] <= 40) indexCol = "#e5bfcc";
var headerinfo = '<th class="'+aid+'"><span class="showindex" style="background-color:'+indexCol+'">'+line[1]+'</span></th>';
var fixdate = dispdate.replace(' ','<br>');
var headerdate = '<th class="'+aid+'">'+fixdate+'</th>';
// skip if already exists
var found = false;
var whichone = false;
$('#headerdate tr th').each(function(idx, item) {
if (fixdate == $(this).html()) {
found = true;
whichone = idx;
}
});
if (!found) {
$.each(data, function (idx, item) {
$('#' + idx).append('<td class="'+aid+'" style="width:70px">' + item + '</td>');
});
$('#headerdate tr').append(headerdate);
$('#headerinfo tr').append(headerinfo);
} else {
$('tr').each(function() {
$('.'+aid).remove();
});
}
}
});
}
});
}

How to complete a Polyline in Leaflet.Draw after clicking second point?

I am facing a problem when I am trying to draw a polyline using the Leaflet.Draw plugin.
First, I click on map to plot first point, and then second click to complete line.
However, after I clicked the line a second time, the line doesn't complete itself. It shows a extension to the line.
When I double-click it, the line completes, or else I need to manually click the finish button. I want to finish that line on second click on map.
This is my code for drawing a Polyline:
var drawControl = new L.Control.Draw({
position: 'topleft',
draw: {
polygon: {
allowIntersection: true,
showArea: true,
drawError: {
color: '#b00b00',
timeout: 1000
},
shapeOptions: {
color: '#0033ff'
}
},
circle: {
shapeOptions: {
color: '#0033ff'
}
},
polyline: {
shapeOptions: {
color: 'red'
},
},
rectangle: {
shapeOptions: {
color: '#0033ff'
}
},
marker: false,
polyline: true,
},
edit: {
featureGroup: drawnItems,
remove: true
}
});
You can override addVertex function from L.Draw.Polyline class, which resides in Leaflet.draw/src/draw/handler/Draw.Polyline.js, using prototype in javascript, and add the following code at the end of it:
markersLength = this._markers.length;
if (markersLength == 2) {
this._fireCreatedEvent();
this.disable();
}
And, here is the full code:
L.Draw.Polyline.prototype.addVertex = function (latlng) {
var markersLength = this._markers.length;
// markersLength must be greater than or equal to 2 before intersections can occur
if (markersLength >= 2 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
this._showErrorTooltip();
return;
}
else if (this._errorShown) {
this._hideErrorTooltip();
}
this._markers.push(this._createMarker(latlng));
this._poly.addLatLng(latlng);
if (this._poly.getLatLngs().length === 2) {
this._map.addLayer(this._poly);
}
this._vertexChanged(latlng, true);
markersLength = this._markers.length;
if (markersLength == 2) {
this._fireCreatedEvent();
this.disable();
}
};
Adding multiple vertices on polylines (e.g., not finishing polylines automatically on the second click) is a feature of Leaflet.Draw.
You may be able to modify this behavior. I recommend that you look at the Leaflet.draw documentation, particularly the L.Draw.Polyline.completeShape() method.
You can trigger the second click automatically to complete the shape.
map.on('draw:drawvertex', e => {
const layerIds = Object.keys(e.layers._layers);
if (layerIds.length > 1) {
const secondVertex = e.layers._layers[layerIds[1]]._icon;
requestAnimationFrame(() => secondVertex.click());
}
});

How to create candlestick chart in plottable.js

What is the best way to create candlestick chart in plottable? I would like to do something like this image on wiki:
Wiki example
I was thinking about using stacked bar plot and styling it with css. For example lowermost value in bar would be transparent, just to position candlestick vertically. Next value in a bar would be lower stick(rectangle horizontally squeezed with css to become line). Next would be the body which is already rectangle and upper stick would again be rectangle squeezed to line.
Is this proper way to go or are there any more elegant solutions? Are there any examples out there? Has anyone done something like this before?
Glad you asked! The best way to do this in Plottable is to combine Plots.Rectangle and Plots.Segment using a Group. Here's an example:
window.onload = function() {
var xScale = new Plottable.Scales.Time();
var yScale = new Plottable.Scales.Linear();
var dataset = new Plottable.Dataset(exampleData);
var wicks = new Plottable.Plots.Segment();
wicks.addDataset(dataset);
wicks.x(function(d) { return parseDate(d.date, 12); }, xScale);
wicks.y(function(d) { return d.high; }, yScale);
wicks.y2(function(d) { return d.low; });
wicks.attr("stroke", "black");
var candles = new Plottable.Plots.Rectangle();
candles.addDataset(dataset);
candles.x(function(d) { return parseDate(d.date, 2); }, xScale);
candles.x2(function(d) { return parseDate(d.date, 22); });
candles.y(function(d) { return d.open; }, yScale);
candles.y2(function(d) { return d.close; });
candles.attr("fill", function(d) {
if (d.close > d.open) {
return "#63c261";
} else {
return "#fd373e";
}
});
var candlesticks = new Plottable.Components.Group([wicks, candles]);
candlesticks.renderTo("#chart");
};
function parseDate(dateString, hourOfDay) {
var day = new Date(dateString);
day.setHours(hourOfDay);
return day;
}
var exampleData = [
{
date: "2014-08-29",
open: 102.86,
high: 102.90,
low: 102.20,
close: 102.50
},
{
date: "2014-08-28",
open: 101.59,
high: 102.78,
low: 101.56,
close: 102.25
},
{
date: "2014-08-27",
open: 101.02,
high: 102.57,
low: 100.70,
close: 102.13
},
];
body { background-color: #AAA; }
svg { background-color: #FFF; }
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.16.2/plottable.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/plottable.js/1.16.2/plottable.css">
</head>
<body>
<svg id="chart" width="400" height="300"></svg>
</body>
</html>
Plots.Segment draws line segments, so setting the ends of each segment to the high/low on each day gets the "wicks" for each candlestick. Meanwhile, Plots.Rectangle draws rectangles, so we set the top/bottom of each rectangle to the open/close on each day. Overlaying the two Plots gets us a candlestick chart.
Hope this helps!