D3: passing object in style method is not working - d3.js

I am using D3 for the exercise. However, I am having trouble passing an object to the .style() method:
var myStyles = [
'#268BD2',
'#BD3613',
'#D11C24',
'#C61C6F',
'#595AB7',
'#2176C7'
];
This below piece of code is working
d3.selectAll('.item')
.data(myStyles)
.style('background',function(d){return d});
But none of the below two code pieces are working
d3.selectAll('.item')
.data(myStyles)
.style({'background':function(d){return d}});
d3.selectAll('.item')
.data(myStyles)
.style({'color':'white','background':function(d){return d}});
Please explain what is wrong here.

You can apply objects in styles and attributes by using d3-selection-multi.
First, you have to reference the mini library:
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>
Then, you have to use styles, not style:
.styles({'color':'white','background':function(d){return d}});
You can see the code working in this fiddle, in which I'm using an object to set the styles: https://jsfiddle.net/gerardofurtado/o54rtrqc/1/
For attributes, use attrs instead of attr.
Here is the API.

I'm also working through the Lynda.com course. Here's an example of the "Binding data to the DOM" exercise using D3 version 4:
JSFiddle
HTML:
<!--
Added these two scripts:
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>
-->
<div class="container">
<h2>D3 Graphic</h2>
<section id="chart">
<div class="item">Darth Vader</div>
<div class="item">Luke Skywalker</div>
<div class="item">Han Solo</div>
<div class="item">Obi-Wan Kenobi</div>
<div class="item">Chewbacca</div>
<div class="item">Boba Fett</div>
</section>
</div>
JS:
var myStyles = [{
width: 200,
color: '#A57706'
}, {
width: 300,
color: '#BD3613'
}, {
width: 150,
color: '#D11C24'
}, {
width: 350,
color: '#C61C6F'
}, {
width: 400,
color: '#595AB7'
}, {
width: 250,
color: '#2176C7'
}];
d3.selectAll('.item')
.data(myStyles)
.styles({
'color': 'white',
'background': function(d) {
return d.color;
},
width: function(d) {
return d.width + 'px';
}
})

Related

How to set the height of CKEditor 5 (Classic Editor)

In CKEditor 4 to change the editor height there was a configuration option: config.height.
How do I change the height of CKEditor 5? (the Classic Editor)
Answering my own question as it might help others.
CKEditor 5 no longer comes with a configuration setting to change its height.
The height can be easily controlled with CSS.
There is one tricky thing though, if you use the Classic Editor:
<div id="editor1"></div>
ClassicEditor
.create( document.querySelector( '#editor1' ) )
.then( editor => {
// console.log( editor );
} )
.catch( error => {
console.error( error );
} );
Then the Classic Editor will hide the original element (with id editor1) and render next to it. That's why changing height of #editor1 via CSS will not work.
The simplified HTML structure, after CKEditor 5 (the Classic Editor) renders, looks as follows:
<!-- This one gets hidden -->
<div id="editor1" style="display:none"></div>
<div class="ck-reset ck-editor..." ...>
<div ...>
<!-- This is the editable element -->
<div class="ck-blurred ck-editor__editable ck-rounded-corners ck-editor__editable_inline" role="textbox" aria-label="Rich Text Editor, main" contenteditable="true">
...
</div>
</div>
</div>
In reality the HTML is much more complex, because the whole CKEditor UI is rendered. However the most important element is the "editing area" (or "editing box") marked with a ck-editor__editable_inline class:
<div class="... ck-editor__editable ck-editor__editable_inline ..."> ... </div>
The "editing area" is the white rectangle where one can enter the text. So to style / change the height of the editing area, it is enough to target the editable element with CSS:
<style>
.ck-editor__editable_inline {
min-height: 400px;
}
</style>
Setting the height via a global stylesheet.
Just add to your common .css file (like style.css):
.ck-editor__editable {
min-height: 500px;
}
In the case of ReactJS.
<CKEditor
editor={ClassicEditor}
data="<p>Hello from CKEditor 5!</p>"
onInit={(editor) => {
// You can store the "editor" and use when it is needed.
// console.log("Editor is ready to use!", editor);
editor.editing.view.change((writer) => {
writer.setStyle(
"height",
"200px",
editor.editing.view.document.getRoot()
);
});
}}
/>
editor.ui.view.editable.editableElement.style.height = '300px';
From CKEditor 5 version 22 the proposed programmatic solutions are not working. Here it is how I get the work done:
ClassicEditor.create( document.querySelector( '#editor' ) )
.then( editor => {
editor.ui.view.editable.element.style.height = '500px';
} )
.catch( error => {
console.error( error );
} );
.ck-editor__editable {min-height: 500px;}
<div>
<textarea id="editor">Hi world!</textarea>
</div>
<script src="https://cdn.ckeditor.com/ckeditor5/22.0.0/classic/ckeditor.js"></script>
Add this to your stylesheet:
.ck-editor__editable {
min-height: 200px !important;
}
If you wish to do this programatically, the best way to do it is to use a Plugin. You can easily do it as follows. The following works with CKEditor 5 version 12.x
function MinHeightPlugin(editor) {
this.editor = editor;
}
MinHeightPlugin.prototype.init = function() {
this.editor.ui.view.editable.extendTemplate({
attributes: {
style: {
minHeight: '300px'
}
}
});
};
ClassicEditor.builtinPlugins.push(MinHeightPlugin);
ClassicEditor
.create( document.querySelector( '#editor1' ) )
.then( editor => {
// console.log( editor );
})
.catch( error => {
console.error( error );
});
Or if you wish to add this to a custom build, you can use the following plugin.
class MinHeightPlugin extends Plugin {
init() {
const minHeight = this.editor.config.get('minHeight');
if (minHeight) {
this.editor.ui.view.editable.extendTemplate({
attributes: {
style: {
minHeight: minHeight
}
}
});
}
}
}
This adds a new configuration to the CKEditor called "minHeight" that will set the editor minimum height which can be used like this.
ClassicEditor
.create(document.querySelector( '#editor1' ), {
minHeight: '300px'
})
.then( editor => {
// console.log( editor );
} )
.catch( error => {
console.error( error );
} );
I tried to set the height and width on the config but it just didn't work on the classic Editor.
I was able to change the height of the editor programmatically on Vue by doing this.
mounted() {
const root = document.querySelector('#customer_notes');
ClassicEditor.create(root, config).then(editor=>{
// After mounting the application change the height
editor.editing.view.change(writer=>{
writer.setStyle('height', '400px', editor.editing.view.document.getRoot());
});
});
}
Use css:
.ck.ck-editor__main .ck-content {
height: 239px;
}
Add this to your global stylesheet, this will increase the size of the CKEditor :)
.ck-editor__editable_inline {
min-height: 500px;
}
Just add it to the style tag.
<style>
.ck-editor__editable
{
min-height: 150px !important;
max-height: 400px !important;
}
</style>
As for configuring the width of the CKEditor 5:
CKEditor 5 no longer comes with a configuration setting to change its width but its width can be easily controlled with CSS.
To set width of the editor (including toolbar and editing area) it is enough to set width of the main container of the editor (with .ck-editor class):
<style>
.ck.ck-editor {
max-width: 500px;
}
</style>
Simply you can add this to your CSS file
.ck-editor__editable {min-height: 150px;}
Put this CSS in your global CSS file and the magic will happen. CkEditor is full of unsolved mysteries.
.ck-editor__editable_inline {
min-height: 400px;
}
Use max-height and min-height both. Beacuse max-height give scroll bar option after reached maximum mention height. Where min-height give static height to <textarea>.
.ck-editor__editable {
max-height: 400px; min-height:400px;}
If its in latest version of Angular say 12 or 12+. We can add below style to your components style file.
:host ::ng-deep .ck-editor__editable_inline { min-height: 300px; }
If you use jQuery and the CKEditor 5 has to be applied to a textarea, there is a "quick and dirty" solution.
The condition:
<textarea name='my-area' id='my_textarea_id'>
If you use jQuery the Editor call could be:
var $ref=$('#my_textarea_id');
ClassicEditor
.create( $ref[0] ,{
// your options
} )
.then( editor => {
// Set custom height via jQuery by appending a scoped style
$('<style type="text/css" scoped>.ck-editor .ck-editor__editable_inline {min-height: 200px !important;}</style>').insertAfter($ref);
} )
.catch( error => {
console.error( error );
} );
In other words, after rendering, you can address the same element used to build the editor and append after a scoped style tag with containing the custom height.
$('<style type="text/css" scoped>.ck-editor .ck-editor__editable_inline {min-height: 200px !important;}</style>').insertAfter($ref);
If you like to use a function (or some class method) to do this, you need something like this:
var editorBuildTo = function(id,options){
var options=options || {};
//Height represents the full widget height including toolbar
var h = options.height || 250; //Default height if not set
var $ref = $('#'+id);
h=(h>40?h-40:h);//Fix the editor height if the toolbar is simple
ClassicEditor
.create( $ref[0] ,{
// your options
} )
.then( editor => {
// Set custom height via jQuery
$('<style type="text/css" scoped>.ck-editor .ck-editor__editable_inline {min-height: '+h+'px !important;}</style>').insertAfter($ref);
} )
.catch( error => {
console.error( error );
} );
}
editorBuildTo('my_textarea_id',{
height:175,
// other options as you need
});
This works well for me
1.resource/assets/js/app.js
=================================
2.paste this code
=================================
require('./bootstrap');
//integrate
window.ClassicEditor = require('#ckeditor/ckeditor5-build-classic');
============================================
3.write on terminal
============================================
npm install --save #ckeditor/ckeditor5-build-classic
npm run watch
=======================================
4.in blade file
=======================================
<!DOCTYPE html>
<html lang="en">
<title></title>
<body>
<form action="{{route('admin.category.store')}}" method="post" accept-charset="utf-8">
#csrf
<div class="form-group row">
<div class="col-sm-12">
<label class="form-control-label">Description:</label>
<textarea name="description" id="editor" class="form-control" row="10" cols="80"></textarea>
</div>
</div>
</form>
<script>
$(function () {
ClassicEditor
.create( document.querySelector( '#editor' ), {
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' }
]
}
} )
.catch( error => {
console.log( error );
} );
})
</script>
</body>
</html>
click to show image here
Building on #Jaskaran Singh React solution. I also needed to ensure it was 100% height to it's parent. I achieved this by assigning a ref called "modalComponent" and further adding this code:
editor.editing.view.change(writer => {
let reactRefComponentHeight = this.modalComponent.current.offsetHeight
let editorToolbarHeight = editor.ui.view.toolbar.element.offsetHeight
let gapForgiveness = 5
let maximizingHeight = reactRefComponentHeight - editorToolbarHeight - gapForgiveness
writer.setStyle(
'height',
`${maximizingHeight}px`,
editor.editing.view.document.getRoot()
)
})
This CSS Method works for me:
.ck-editor__editable {
min-height: 400px;
}
I resolve this just adding in my layout page
<style>
.ck-content{
height: 250px;
}
</style>
Hope i help someone :D
For this particular version https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js,
the below code block worked for me.
.cke_contents { height: 500px !important; }
I guess the difference is just the fact that is it in plural.
In my case it worked for me
Add a ck class and write style like below:
<style>
.ck {
height: 200px;
}
</style>
Using plugin here I came up with this
let rows: number;
export class MinHeightPlugin {
constructor(public editor) {
}
init = function () {
this.editor.ui.view.editable.extendTemplate({
attributes: {
style: {
minHeight: (rows * 40) + 'px',
}
}
});
};
}
export const MinHeightPluginFactory = (rowss: number): typeof MinHeightPlugin => {
rows = rowss;
return MinHeightPlugin;
};
and the usage(4 rows each rows is considered 40px height):
this.editor.builtinPlugins.push(MinHeightPluginFactory(4));
I couldn't manage to make rows variable local to MinHeightPlugin, does anyone know how to do it?
.ck-editor__editable_inline {
min-height: 400px;
}
This makes height change for every editor used across all components. So it doesn't work in my case.
In Case of react js
<CKEditor
toolbar = {
[
'heading',
'bold',
'Image'
]
}
editor={ClassicEditor}
data={this.state.description}//your state where you save data
config={{ placeholder: "Enter description.." }}
onChange={(event, editor) => {
const data = editor.getData();
this.setState({
description : data
})
}}
onReady={(editor)=>{
editor.editing.view.change((writer) => {
writer.setStyle(
//use max-height(for scroll) or min-height(static)
"min-height",
"180px",
editor.editing.view.document.getRoot()
);
});
}}
/>
In order to enable both rich text editor and source mode to have the same height, use the following CSS:
.ck-source-editing-area,
.ck-editor__editable {
min-height: 500px;
}
.ck-editor__main {
height: 500px;
min-height: 500px;
max-height: 500px;
overflow-y: scroll;
border: 1px solid #bbbbbb;
}
Just test it's work. Hoping help you
var editor_ = CKEDITOR.replace('content', {height: 250});

Using a Query to retrieve data from a googlesheet and push it into a dashboard

I am trying to withdraw data from a google spreadsheet, convert it into a DataTable and push the values into a Dashboard LineChart according to Google Apps documentation. I have managed to do this with an array I manually populated but I have not managed to figure out how to do this properly with a spreadsheet. It currently states that one or more participants failed to draw and I have an invalide column label. Any ideas what I am doing wrong.
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart', 'controls']});
google.charts.setOnLoadCallback(drawStuff);
function initialize() {
var opts = {sendMethod: 'auto'};
// Replace the data source URL on next line with your data source URL.
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1bqRWtvB6kxBfeIuVzCwVXZ5_mCn1b542CaIwE116jK8/edit#gid=0', opts);
query.setQuery("Select *");
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()){
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
else {
var data = response.getDataTable();
return data;
}
}
function drawStuff() {
var data = initialize;
var dashboard = new google.visualization.Dashboard(
document.getElementById('programmatic_dashboard_div'));
// We omit "var" so that programmaticSlider is visible to changeRange.
programmaticSlider = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'programmatic_control_div',
'options': {
'filterColumnLabel': 'Query',
'ui': {'labelStacking': 'vertical'}
}
});
programmaticChart = new google.visualization.ChartWrapper({
'chartType': 'LineChart',
'containerId': 'programmatic_chart_div',
'options': {
'width': 750,
'height': 300,
'legend': {position: 'top', textStyle: {color: 'blue', fontSize: 16}},
'chartArea': {'left': 15, 'top': 15, 'right': 0, 'bottom': 0},
'chart': {
title: 'Popular Queries',
subtitle: 'by number of clicks'
},
}
});
dashboard.bind(programmaticSlider, programmaticChart);
dashboard.draw(data);
}
</script>
</head>
<body>
<div id="programmatic_dashboard_div" style="border: 1px solid #ccc">
<table class="columns">
<tr>
<td>
<div id="programmatic_control_div" style="padding-left: 2em; min-width: 250px"></div>
<div>
<button style="margin: 1em 1em 1em 2em" onclick="changeRange();">
Select range [2, 5]
</button><br />
</div>
<script type="text/javascript">
function changeRange() {
programmaticSlider.setState({'lowValue': 2, 'highValue': 5});
programmaticSlider.draw();
}
</script>
</td>
<td>
<div id="programmatic_chart_div"></div>
<div id="chart_div"></div>
</td>
</tr>
</table>
</div>
</body>
</html>

Kendo Display Multiple Bar Charts on Web Page

I'm using Kendo UI and trying to display multiple charts on a single web page. Everything works until I try to add a second bar chart. One of the charts does not display and just shows a generic chart image (it looks like it is not finding the data but if I reorder they reverse what is happening). I can display multiple line charts. Any ideas about what might be happening?
Below is my html for the two charts without the SVG info:
<div kendo-chart="" k-options="vm.barChartOptions" ng-show="this.dataItem.visible" class="move k-block ng-scope k-chart" id="costPerPound" style="float: left; margin: 5px 0px; position: relative;" data-uid="b543ff9a-ee57-4ce7-a39d-8f69a0505a2b" role="option" aria-selected="false" data-role="chart"></div>
<div kendo-chart="" k-options="vm.barChartOptions" ng-show="this.dataItem.visible" class="move k-block ng-scope k-chart" id="numShipments" style="float: left; margin: 5px 0px; position: relative;" data-uid="97094bf1-4366-4974-b92f-edf36d1980f4" role="option" aria-selected="false" data-role="chart"></div>
Here are is the k-options info. As you can see I am setting most of my information at render.
vm.barChartOptions = {
dataSource: vm.chartData_datasource,
series: [
{
}
],
valueAxis: {
line: {
visible: false
},
labels: {
rotation: "auto"
}
},
tooltip: {
visible: true,
template: "#= series.name #: #= value #"
},
render: function (e) {
var chart = e.sender;
var chartData = vm.findChartData(e);
if (chartData != null) {
chartData.categoryAxisField = vm.firstToLower(chartData.categoryAxisField);
chart.options.title.text = chartData.title;
chart.options.name = chartData.htmlID;
chart.options.categoryAxis.field = chartData.categoryAxisField;
chart.options.categoryAxis.labels.format = chartData.categoryAxisLabel;
chart.options.legend.position = chartData.legendPosition;
chart.options.seriesDefaults.type = chartData.chartType;
for (var i = 0; i < chart.options.series.length; i++) {
chart.options.series[i].type = chartData.chartType;
chart.options.series[i].field = vm.firstToLower(chartData.dataField);
}
}
}
}
I had same issue. It can be resolved by just providing different name
to each chart.

In AmChart map how to trigger event manually?

In AmMap, "homeButtonClicked" event is present which got triggered when you click on home button.
I want to trigger that event manually.
Is there any way to do so?
To completely zoom out the map, use zoomToSelectedObject() ant pass in map.dataProvider as a parameter.
I.e.:
map.zoomToSelectedObject(map.dataProvider);
Working demo:
var map = AmCharts.makeChart( "chartdiv", {
"type": "map",
"dataProvider": {
"map": "worldLow",
"getAreasFromMap": true
},
"areasSettings": {
"autoZoom": true
}
} );
function zoomOut() {
map.zoomToSelectedObject(map.dataProvider);
}
#chartdiv {
width: 100%;
height: 250px;
}
<script src="http://www.amcharts.com/lib/3/ammap.js"></script>
<script src="http://www.amcharts.com/lib/3/maps/js/worldLow.js"></script>
<input type="button" value="Zoom Out" onclick="zoomOut();" />
<div id="chartdiv"></div>
To trigger an event on the map or any amCharts object for that matter, you can use fire() method. I.e.:
map.fire("homeButtonClicked", {
type: "homeButtonClicked",
chart: map
});
Here's a working example:
var map = AmCharts.makeChart( "chartdiv", {
"type": "map",
"dataProvider": {
"map": "worldLow",
"getAreasFromMap": true
},
"areasSettings": {
"autoZoom": true
}
} );
map.addListener("homeButtonClicked", function(event) {
alert('homeButtonClicked');
});
function testEvent() {
map.fire("homeButtonClicked", {
type: "homeButtonClicked",
chart: map
});
}
#chartdiv {
width: 100%;
height: 280px;
}
<script src="http://www.amcharts.com/lib/3/ammap.js"></script>
<script src="http://www.amcharts.com/lib/3/maps/js/worldLow.js"></script>
<input type="button" value="trigger event" onclick="testEvent();" />
<div id="chartdiv"></div>
It seems the code has changed since this was last answered. For me, this is what works.
map.clickMapObject(map.dataProvider)
(For martynasma) You still have an error on trigger event method (testEvent). Correct is:
map.fire({
type: "homeButtonClicked",
chart: map
});

Simple SlickGrid's Data not displaying

Just started using SlickGrid, Not sure if I am missing something, but this simple code doesn't seem to work. It is displaying headers, but not the rows.
Grid Code:
var grid;
var data = [
{ ID: 1, VALUE: "Value1"},
{ ID: 2, VALUE: "Value2"},
{ ID: 3, VALUE: "Value3"},
{ ID: 4, VALUE: "Value4"},
{ ID: 5, VALUE: "Value5"}
];
var columns = [
{name:'ID NUM', field:'ID', id:'ID'},
{name:'VALUE', field:'VALUE', id:'VALUE'}
];
var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
grid = new Slick.Grid("#container-div", data, columns, options);
grid.render();
For some reason, only first row's first cell is rendered in html and is not been displayed.
HTML:
<div class="slick-viewport" style="width: 100%; overflow: auto; outline: 0px none; position: relative; height: 0px;">
<div class="grid-canvas" style="height: 150px; width: 880px;">
<div class="ui-widget-content slick-row even" style="top:0px">
<div class="slick-cell l0 r0">1</div>
</div>
</div>
</div>
You are missing the container div to display the data, in your javascript code you are referencing #container-div but it does not exist inside your html code. Typically you don't have to write any html code like you seem to be doing (or is it the result you displayed?), except for the grid container itself. Try simply adding or replacing with this code in your html body: <div id="container-div" style="width:600px;height:500px;"></div> You can see a simple example from SlickGrid here: https://github.com/mleibman/SlickGrid/blob/master/examples/example1-simple.html

Resources