同一页面上的多个图表(chartjs),构造函数/原型模式代码组织

时间:2016-04-28 11:47:05

标签: javascript jquery charts

您好我正在测试多个图表/图表在同一页面上的情况,目前我有2个用于测试目的但稍后会有更多。说,我使用构造函数/原型模式,而一旦点击按钮,页面上显示的图表,如果我正确组织代码,我想知道没有错误。

这是代码示例

var showGraphs = (function () {

function showGraphs () {
}

//grahp1
var ctx1 = document.getElementById("myGraph1").getContext("2d");
var data1 = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            backgroundColor: "rgba(255,99,132,0.2)",
            borderColor: "rgba(255,99,132,1)",
            borderWidth: 1,
            hoverBackgroundColor: "rgba(255,99,132,0.4)",
            hoverBorderColor: "rgba(255,99,132,1)",
            data: [65, 59, 80, 81, 56, 55, 40],
        }
    ]
};


//graph2
var ctx = document.getElementById("myGraph2").getContext("2d");
var randomScalingFactor = function() {
    return Math.round(Math.random() * 100);
};
var randomColorFactor = function() {
    return Math.round(Math.random() * 255);
};
var randomColor = function(opacity) {
    return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
};

var config = {
    type: 'doughnut',
    data: {
        datasets: [{
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
            ],
            backgroundColor: [
                "#F7464A",
                "#46BFBD",
                "#FDB45C",
                "#949FB1",
                "#4D5360",
            ],
            label: 'Dataset 1'
        }, {
            hidden: true,
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
            ],
            backgroundColor: [
                "#F7464A",
                "#46BFBD",
                "#FDB45C",
                "#949FB1",
                "#4D5360",
            ],
            label: 'Dataset 2'
        }, {
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
            ],
            backgroundColor: [
                "#F7464A",
                "#46BFBD",
                "#FDB45C",
                "#949FB1",
                "#4D5360",
            ],
            label: 'Dataset 3'
        }],
        labels: [
            "Red",
            "Green",
            "Yellow",
            "Grey",
            "Dark Grey"
        ]
    },
    options: {
        responsive: true,
        legend: {
            position: 'top',
        },
        title: {
            display: true,
            text: 'Chart.js Doughnut Chart'
        },
        animation: {
            animateScale: true,
            animateRotate: true
        }
    }
};


showGraphs.prototype.myAllGraphs = function () {

    //graph1
    var myGraph1 = new Chart(ctx1, {
    type: 'bar',
    data: data1
    });

    //graph2
    var myDoughnut = new Chart(ctx, config);

}

return showGraphs;


})();

var graphs = new showGraphs ();
$("#testButton").on("click",graphs.myAllGraphs);

这里是FIDDLE

请告诉我代码组织是否有问题。

1 个答案:

答案 0 :(得分:0)

1-我会将数据提取到另一个文件。它可以消除此文件的混乱,并可以在其他地方重复使用

2-对于2个按钮,您可以让 package com.ebookfrenzy.generatekey; import android.app.KeyguardManager; import android.content.Context; import android.security.keystore.KeyProperties; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.hardware.fingerprint.FingerprintManager; import android.util.Base64; import android.widget.Toast; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyPermanentlyInvalidatedException; import java.security.KeyStore; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.cert.CertificateException; import java.security.InvalidAlgorithmParameterException; import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyStoreException; import java.security.UnrecoverableKeyException; import javax.crypto.KeyGenerator; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.Cipher; public class MainActivity extends AppCompatActivity { private static final String KEY_NAME = "example_key"; private FingerprintManager fingerprintManager; private Cipher cipher; private FingerprintManager.CryptoObject cryptoObject; private KeyStore keyStore; private KeyGenerator keyGenerator; byte[] En_Key; private String encrypted; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); generateKey(); if(cipherInit()) { cryptoObject=new FingerprintManager.CryptoObject(cipher); encrypted=byteArraytoHex(En_Key); Toast.makeText(this,encrypted, Toast.LENGTH_SHORT).show(); } } protected void generateKey() { try { keyStore = KeyStore.getInstance("AndroidKeyStore"); } catch (Exception e) { e.printStackTrace(); } try { keyGenerator = KeyGenerator.getInstance( KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); } catch (NoSuchAlgorithmException | NoSuchProviderException e) { throw new RuntimeException( "Failed to get KeyGenerator instance", e); } try { keyStore.load(null); keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setUserAuthenticationRequired(false) .setEncryptionPaddings( KeyProperties.ENCRYPTION_PADDING_PKCS7) .build()); keyGenerator.generateKey(); } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | CertificateException | IOException e) { throw new RuntimeException(e); } } public boolean cipherInit() { try { cipher = Cipher.getInstance( KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new RuntimeException("Failed to get Cipher", e); } try { keyStore.load(null); SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null); En_Key=key.getEncoded(); cipher.init(Cipher.ENCRYPT_MODE, key); return true; } catch (KeyPermanentlyInvalidatedException e) { return false; } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) { throw new RuntimeException("Failed to init Cipher", e); } } public static String byteArraytoHex(byte[] array) { StringBuffer hexStr=new StringBuffer(); for(byte b:array) { int inval = b & 0xff; if (inval < 0x10) { hexStr.append("0"); } hexStr.append(Integer.toHexString(inval)); } return hexStr.toString(); } 使用参数来定义您应该显示的图形,例如:

showGraphs