如何在chartjs-node中使用插件?

时间:2019-01-23 20:28:33

标签: javascript node.js chart.js

我正在使用chartjs-node在服务器端将图表转换为图像。它生成没有插件的图像。但是我需要使用一些插件,例如注释和动画。我想我不知道使用插件。这就是问题。 请帮助我在chartjs-node中使用插件。 预先感谢

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题,我设法解决了这个问题: 这是chartjs-plugin-annotation

的示例

我忘记了在# Load the dataset as DataFrame import pandas as pd from sklearn.datasets import load_iris df = pd.DataFrame(load_iris().data, columns=load_iris().feature_names) df['label'] = load_iris().target # Divide to X and y and split to train and test sets X = df.iloc[:,0:4].values y = df.iloc[:,4].values from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) # Define the neural network model from keras.models import Sequential from keras.layers import Dense def baseline_model(): model = Sequential() model.add(Dense(units=8, activation='relu', input_dim=4)) model.add(Dense(units=3, activation='sigmoid')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy']) return model # Wrap the model from keras.wrappers.scikit_learn import KerasClassifier my_model = KerasClassifier(build_fn=baseline_model) my_model.fit(X_train, y_train, batch_size=4, epochs=100, verbose=0) # Run eli5 explanations import eli5 from eli5.sklearn import PermutationImportance perm = PermutationImportance(my_model, random_state=1).fit(X_test, y_test) # This actually works! explanation = eli5.formatters.as_dataframe.explain_weights_df(perm, feature_names=df.columns[:-1].tolist()) # But this line does not "work", i.e. it produces a NoneType object explanation_pred = eli5.formatters.as_dataframe.explain_prediction_df(estimator=my_model, doc=X_test[0]) 事件中注册插件

beforeDraw

以及您的配置(示例):

const ChartjsNode = require('chartjs-node');
const ChartjsAnnotation = require('chartjs-plugin-annotation');

var chartNode = new ChartjsNode(width, height);
chartNode.on('beforeDraw', function (Chartjs) {

    // Register the annotation plugin
    Chartjs.plugins.register( ChartjsAnnotation );
});

chartNode.drawChart( <your_chartjs_configuration> )
.then( function(){
    // Save as testimage.png
    chartNode.writeImageToFile('image/png', './testimage.png'); 
});