我尝试制作使用electron实时图表库的Epoch桌面应用。我让电子应用程序运行良好并在基本节点环境中制作了一些Epoch图表但是当我尝试将电子和纪元结合起来时我得到了一个错误:未捕获TypeError:$(...)。epoch不是函数。
我尝试从index.html加载 jquery.js , d3.js 和 epoch.js 模块(代码示例1)或者从javascript(代码示例2)中获取它们。
<!--Code example 1-->
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/d3.min.js"></script>
<script type="text/javascript" src="./js/epoch.min.js"></script>
//Code example 2
var $ = require('jquery');
var d3 = require('d3');
var epoch = require('epoch-charting');
我打开电子的app.js如下:
var app = require('app');
var BrowserWindow = require('browser-window');
var mainWindow = null;
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('ready', function() {
mainWindow = new BrowserWindow({
width: 600,
height: 300,
'min-width': 500,
'min-height': 200,
'accept-first-mouse': true,
'title-bar-style': 'hidden'
});
mainWindow.loadUrl('file://' + __dirname + '/index.html');
mainWindow.openDevTools();
mainWindow.on('closed', function() {
mainWindow = null;
});
});
和index.html:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<link rel="stylesheet" href="./css/epoch.min.css">
</head>
<body>
<div id="lineChart" class="epoch" style="width: 100%; height: 400px;border:solid 1px #C0C0C0;"></div>
<script>
var $ = require('jquery');
var d3 = require('d3')
var epoch = require('epoch-charting');
var lineChartData = [
{
label: "Series 1",
values: [ {x: 0, y: 100}, {x: 20, y: 1000}]
},
{
label: "Series 2",
values: [ {x: 20, y: 78}, {x: 30, y: 98}]
}
];
$('#lineChart').epoch({
type: 'line',
data: lineChartData
});
</script>
</body>
</html>
答案 0 :(得分:0)
您需要将jQuery对象传递到窗口上下文,以便将epoch-charting模块“工作”
global.jQuery = require('jquery');
require('epoch-charting');
jQuery('#chart').epoch({type: 'time.line', data: data});