Skip to content

Commit

Permalink
Fix #30
Browse files Browse the repository at this point in the history
  • Loading branch information
ae3e committed Apr 30, 2021
1 parent 9defe28 commit 51673b3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/PanelOptionCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export const PanelOptionCode: React.FC<Props> = ({ value, item, onChange }) => {
}
return <CodeEditor
language={item.settings?.language}
showLineNumbers={item.settings?.language==='javascript'?true:false}
value={value==='null'?JSON.stringify(item.settings?.initValue,null,2):value}
height="200px"
onBlur={code => {
if(item.settings?.language==='json' && code){
code=JSON.parse(code);
}
}
onChange(code)}
}
/>;
Expand Down
125 changes: 74 additions & 51 deletions src/SimplePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface Props extends PanelProps<SimpleOptions> {}

export class SimplePanel extends PureComponent<Props> {
render() {

//Get all variables
const context = {
//interval: templateSrv.getBuiltInIntervalValue(),//dataSource.templateSrv.builtIns.__interval.value,
Expand All @@ -36,71 +37,93 @@ export class SimplePanel extends PureComponent<Props> {
templateSrv.getVariables().forEach((elt: any)=>{
context[elt.name]=elt.current.text;
})
let parameters: any;


//const NbValues = data.series[0].rows.length;

let config = this.props.options.config || defaults.config
let data = this.props.options.data || defaults.data
let layout = this.props.options.layout || defaults.layout;
let frames = this.props.options.frames || defaults.frames;

let parameters: any;
parameters = [this.props.options.data,layout,config];

let error: any;
try {
if (this.props.options.script !== '') {
if (this.props.options.script !== '' && this.props.data.state !== 'Error') {
var f = new Function('data,variables', this.props.options.script);
parameters = f(this.props.data, context);
}else{
parameters = [this.props.options.data,layout,config];
if(!parameters){
throw new Error('Script must return values');
}
}
} catch (e) {
console.log(e);
}
error = e;
console.error(e);

//Can't update chart when script is changing if throw error?!?
//throw new Error('There\'s an error in your script. Check the console to see error\'s details');

const combineMerge = (target, source, options) => {
const destination = target.slice()

source.forEach((item, index) => {
if (typeof destination[index] === 'undefined') {
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
} else if (options.isMergeableObject(item)) {
destination[index] = merge(target[index], item, options)
} else if (target.indexOf(item) === -1) {
destination.push(item)
}
})
return destination
}
//Merge data field and data transformed by script
/*let series: any[] = [];
if (data2.length && data2.length > 0) {
data2.forEach((serie, index) => {
let options = this.props.options.data[index];
series.push({
...options,
...data2[index],




const combineMerge = (target, source, options) => {
const destination = target.slice()

source.forEach((item, index) => {
if (typeof destination[index] === 'undefined') {
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
} else if (options.isMergeableObject(item)) {
destination[index] = merge(target[index], item, options)
} else if (target.indexOf(item) === -1) {
destination.push(item)
}
})
return destination
}
//Merge data field and data transformed by script
/*let series: any[] = [];
if (data2.length && data2.length > 0) {
data2.forEach((serie, index) => {
let options = this.props.options.data[index];
series.push({
...options,
...data2[index],
});
});
});
}*/

layout = {...layout, autosize: true, height: this.props.height};
return (
<Plot
style={{
width: '100%',
height: '100%',
}}
data={parameters.data?merge(data,parameters.data,{ arrayMerge: combineMerge }):data}
frames={parameters.frames?merge(data,parameters.frames,{ arrayMerge: combineMerge }):frames}
onInitialized={(figure: any, graphDiv: any) => this.setState({ figure: figure, graphDiv: graphDiv })}
//layout={ {autosize:true, height:this.props.height, title: this.props.options.title} }
layout={parameters.layout?merge(layout,parameters.layout):layout}
config={parameters.config?merge(config,parameters.config):config}
useResizeHandler={true}
onClick={data=>{
//console.log(data)
var f = new Function('data', this.props.options.onclick);
f(data);
}}
></Plot>
);
}*/

layout = {...layout, autosize: true, height: this.props.height};
let display: any;
if(error){
let matches = error.stack.match(/anonymous>:.*\)/m);
let lines = matches?matches[0].slice(0,-1).split(':'):null
display = <div >There's an error in your script : <br/><span style={{color:'#D00'}}>{ error.toString()}</span> {lines?"- line "+(parseInt(lines[1])-2)+":"+lines[2]:""} (Check your console for more details)</div>
}else{
display = <Plot
style={{
width: '100%',
height: '100%',
}}
data={parameters.data?merge(data,parameters.data,{ arrayMerge: combineMerge }):data}
frames={parameters.frames?merge(data,parameters.frames,{ arrayMerge: combineMerge }):frames}
onInitialized={(figure: any, graphDiv: any) => this.setState({ figure: figure, graphDiv: graphDiv })}
//layout={ {autosize:true, height:this.props.height, title: this.props.options.title} }
layout={parameters.layout?merge(layout,parameters.layout):layout}
config={parameters.config?merge(config,parameters.config):config}
useResizeHandler={true}
onClick={data=>{
//console.log(data)
var f = new Function('data', this.props.options.onclick);
f(data);
}}
></Plot>

}
return display;

}
}

0 comments on commit 51673b3

Please sign in to comment.